あなたの問題は小枝の問題ではありません。小枝のみがテンプレートエンジンであり、ページの最初のレンダリングでのみ使用されます。あなたがすべきことは、ajax保存から成功した応答を得たときに、jqueryを使用して新しいDOM要素を作成し、同じスキーマを使用してページに追加することです. 何かのようなもの:
<script>
$.post(
TheUrlThatYouPostTo,
TheDataThatYouPassWithThePost,
function(returnData, textStatus){
//assuming that your return data is an array conatianing a boolean successful key
if(returnData.successful){
$('#YourCommentsWrapperElement').append('<div class="comment">#some other elements containing the posted values</div>');
}
}
);
</script>
または、twig にコメント HTML を生成させたいが、それを JavaScript にハードコーディングしたくない場合は、AJAX 投稿で投稿しているコントローラー アクションに、レンダリングされたバージョンのコメントを返すようにさせることができます。このようなもの:
#Your Controller file
public function ajaxPostAction(Request $request){
//Handle post data to create a new commentObject
return array(
'comment' => $commentObject
);
}
次に、そのアクションのビュー ファイルを作成します。
#your view for the controller action ajaxPost.html.twig
<div class="comment">
<h3>{{comment.author}}</h3>
<p>{{comment.content}}</p>
</div>
次に、JavaScript は戻りデータを取得し、既存のリストに追加します。
<script>
$.post(
TheUrlThatYouPostTo,
TheDataThatYouPassWithThePost,
function(returnData, textStatus){
//Now your return data is the HTML for the new comment so just append it to the rest
$('#YourCommentsWrapperElement').append(returnData);
}
);
</script>