How to update an array in TWIG template? - php

Currently, I am using TWIG to display all my information(articles and comments). So, I first retrieve the articles and comments from my database and pass it into an array. This is then passed into my twig template. Then, I load each article and underneath each article I load in the comments of that article. These comments and the buttons associated with it(aka the new comment button) are all made in my macro.
The macro just gets the array that contains the comments that I need to display. It display the comments for the article and with each comment it makes a reply button, so the user can reply to any comment or start a new thread.
I have used AJAX and jQuery to store the new comments in my database without refreshing the page. However, I need to figure out a way to display the new comment without refreshing the whole page. Does anyone have any idea on how to do this in TWIG?
The problem I am facing with TWIG right now is how to post comments asynchronously. I load the array of the current database of comments initially when I load the page. However, if my database updates, how do I update my array with it. Any suggestions? I was thinking of grabbing my array whenever a new comment is added, which is why my question is "How to retrieve an array in TWIG template?", but I am open to other suggestions.

Your issues isnt a twig issue, twig only is a templating engine and is only used on the initial render of the page. What you should be doing is when you get a successful response from your ajax save is create a new DOM element using jquery and add it to the page using your same schema. Something like:
<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>
Or if you want twig to generate the comment HTML for you and you dont want to have it hard coded into your javascript you can have the controller action that your are posting to with the AJAX post return a rendered version of the comment. Something like this:
#Your Controller file
public function ajaxPostAction(Request $request){
//Handle post data to create a new commentObject
return array(
'comment' => $commentObject
);
}
Then you create a view file for that action:
#your view for the controller action ajaxPost.html.twig
<div class="comment">
<h3>{{comment.author}}</h3>
<p>{{comment.content}}</p>
</div>
Then your javascript will just take the return data and append it to the existing list:
<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>

Related

Pass Variable to Div with AJAX

ORIGINAL POST: Not sure why I am having such a hard time grasping this but I am creating a page that executes a query to list a set of records per a user. I am wanting to create a div that shows the details of a select record from the list. I am attempting to load that record with the following:
$(document).ready(function(){
// load index page when the page loads
$("#form").load("tm-reserves-form.php");
$("#record").click(function(){
// load home page on click
$("#form").load("tm-reserves-form-test.php?ContactID"+$ContactID);
});
});
Initial form loads but I can not get my .click function to work using the URL parameters.
How can I pass the url parameter from my current query into a div that loads an external page and executes a second query based on that url paramenter (primary id)?
Thanks in advanced for helping me out!
EDIT POST: Found solution, but another issue has arisen.
So I took a crash course this weekend on ajax and I found a working solution here:
$(document).ready(function(){
// load index page when the page loads
$("#form").load("tm-reserves-form.php");
$("#record").click(function(){
// load home page on click
var ContactID = document.getElementById('ContactID').value;
$("#form").load("tm-reserves-form.php", {"ContactID": ContactID});
});
});
So with this example, when the page loads the div form is loaded with a default page. With a click function on the label #record the div #form is than loaded with a parameter of ContactID that is a string value inside of #ContactID. All that seems to work very well (even passes the parameter correctly).
The issue now is that it is only allowing me to pass the first record to my div. Any thoughts on what I need to implement next? I originally thought it was to clear the var but that didn't work either.
The original query parameters are not readily available to a running script. So $ContactID is not resolved to an actual value.
See this page on methods for extracting query parameters from the current page:
Get query string parameters with jQuery
$(document).ready(function(){
// load index page when the page loads
$("#form").load("tm-reserves-form.php");
$("#record").click(function(){
// load home page on click
var ContactID = document.getElementById('ContactID').value;
$("#form").load("tm-reserves-form.php", {"ContactID": ContactID});
});
});
This code solved my initial question, but led to an additional question.

Showing a lister when grid button clicked? (in Agile Toolkit)

I have a grid that have buttons in one of it's columns like this:
how can I show a lister or a new grid when the button clicked?
$grid=$page->add('Grid');
$grid->setModel('Tickets',array('subject','date','time','department','status','text'));
$grid->addColumn("button",'read_ticket_id','Read');
if($_GET['read_ticket_id']){
// this generates javascript to be executed on buttion click
//how can I show a lister or a new grid when the button clicked?
}
Check out examples in ATK4 Codepad.
http://agiletoolkit.org/codepad/gui/grid
Edit:
This is snippet from one of my pages. Maybe you can find it useful.
The idea behind this is that you actually generate JavaScript inside this IF statement and JavaScript then is sent back to your browser which then can make another request for something (reload existing object, create new, redirect to somewhere etc.)
...
if($_GET['ticket']){
// Join this report with selected ticket
$this->grid->model->addToTicket($_GET['ticket']);
// Reload
$this->js(null,array(
$x->js()->reload(),
$this->js()->univ()->successMessage('Successfully saved')
))->execute();
}
...
With $_GET['ticket'] you get ID of record in grid in which you clicked button "Add to Ticket". $x is some other object in this page, for example, some form, field, tab or other grid. With $this->grid->model you get reference to model associated with this grid and in that model I have custom action/method defined - addToTicket which do something with database.
You can also redirect to other page with $this->js()->redirect() or $this->js()->location() etc. Basically you can do whatever you want, but all of this need to generate JavaScript as result or instructions for your browser what to do next.
And don't forget to add ->execute() at the end! That will stop further parsing your page and will instantly generate JS response.
I found a good example for this question:
http://agiletoolkit.org/doc/grid/interaction
==========
$g=$p->add('Grid');
$g->setSource('user');
$g->addColumn('name');
$g->addColumn('surname');
$g->addColumn('button','info','More Info');
$g->dq->where('name is not null')->limit(5);
if($_GET['info']){
$g->js()->univ()->dialogURL('More info',
$this->api->getDestinationURL(
null,array(
'more_info'=>$_GET['info'],
'cut_object'=>'myform'
)))
->execute();
}
if($_GET['more_info']){
$f=$this->add('Form','myform');
$f->addField('readonly','name');
$f->addField('readonly','surname');
$f->setSource('user');
$f->setConditionFromGET('id','more_info');
}

Yii eexcelview not exporting grid filters

I am trying to export cgridview data (current search results) to a CSV by using the excelview extension, by clicking a button.
However, I am not getting the filtered data in my CSV; instead, I'm getting all the records in the model.
This is my controller action I am calling:
public function actionExcel()
{
$model = new PackagingMetric('search');
$this->widget('application.extensions.EExcelView', array(
'dataProvider'=> $model->search(),
'grid_mode'=>'export',
'exportType'=>'Excel5',
'filename'=>'report',
));
}
Would anyone know how to resolve this issue, or where my error is ?
You don't have any search criteria there. Usually you do it like this:
$model = new PackagingMetric('search');
$model->attributes = $_POST['PackagingMetric']; // this would be the model associated with the search form
$model->search();
I don't see anything in your code that takes values from user input, so you should use your search form somehow. Of course, you could have default values in your model, but I don't think that's the case.
Edit:
I looked a bit more into the source code of the extension you are using. It seems that the export buttons are actually standard <a href=''></a> links that send you to a page that's supposed to output the Excel sheet. However, because the actual filter data is not transmitted to that page, there's no way to apply those filters server-side.
Since this is the intended behavior of the extension, there's no elegant way to solve it, but there are messy ways.
A first option would be to tweak the extension yourself, but this will break compatibility with future versions. Another way is to use Javascript to send your data where you need it. In the view file where you are displaying the gridview, you should wrap the widget in a form element or in an ActiveForm with all validation disabled. Then you need to place the following piece of Javascript code somewhere on the page:
var $excelForm = $( '#your_form_id' );
$( '.summary a', $excelForm ).click( function() {
$excelForm.attr( 'action', this.href ).submit();
return false;
});
This will submit the form to the address specified by the export link. Because the form wraps the widget, it will contain the input elements that filter your results, so you will have access to the user-entered filters on the server. You can use the code I originally posted to pick up the data from there.
Note: I am using the default class name for the div that contains the export buttons, which is .summary. If you are using a different class, you should change the respective JS code.

Access html elements data and send to controller in PHP

I'm a super beginner in php, and I'm using a MVC php framework called Yii. I can't seem to find any articles that explain how to get values of html elements with PHP. Everywhere I look it's all about how to get values from form fields after a POST in some other view. Is there anyway to get field values and send them to a controller in PHP and just come back to the original view.
In .Net MVC I just use jquery to get form fields and do an ajax call. It's not sensitive data so I'm not worried about security. I like ajax because I don't do any page post back, I just send my data over and remain on the same page I was on.
Is there any way to do MVC AJAX kind of thing with PHP? Read html element values and send them to a controller for data manipulation?
It works the same way. Yii comes bundled with jquery, so you
just use jquery to get form fields and do an ajax call
to some controller function, do whatever you want with it, and return a response, with php's echo.
If you already know some jquery, then the client-side shouldn't be much different from .net mvc.
Edit:
To add a <script> to the generated html see registerScript.
To create urls use the createUrl function.
To add ajax options to html tags code looks similar to:
echo CHtml::checkBox('mybox',false,
array(// array for htmloptions, we also pass ajax options in here
'class'=>'checkBoxes_class',
'ajax'=>array(// this is ajax options for jquery's ajax
'type'=>'POST',
'url'=>Yii::app->createUrl('xyz',array('clickedboxid'=>'mybox')), // here you passed clickedboxid as a get variable
'beforeSend'=>'function(){}',
'success'=>'',
// etc etc
)
)
);
Every html tag generator helper function takes htmlOptions array, where we can also pass ajax options.
While reading these values in the controller:
public function actionSomeAction($id){
// $id is mybox
echo "Hello"; // this is returned as response to the client
}
Hope this is enough for you to get started.

call ajax inside wordpress posts/pages

Sorry for this but I searched the whole web for a solution to my probleme but in vain :(
What I want to achieve is creating a normal Post and adding a form to it that once submitted, goes to a database and gets back a value.
I created a plugin for that and integrated it in the admin menu then set a function that queries the db :
myfunc_getcode($db, $table, $value, $return) // returns a value
how can I achieve this!? I mean, when a user inserts some data in the form (that exists inside a post or page) then he clicks on submit, Ajax talks to the db and gets the results back.
I don't even know if wordpress 3.0.1 allows such things!
I got it to work by using
add_action('the_content', 'my_function');
this hooks my plugin to the posts and pages.
then in the function I transmit the content like;
function my_function($content) {}
Then I used Ajax by integrating jquery inside my script;
<script type="text/javascript">
jQuery(document).ready(function($) {}
for submitting forms I used jquery/ajax to listen to the form
$("#my_form_id").submit(function() {
and used jquery's $.post to pass variables to a php page that handeles my results and returns them by echo.
hope this helps someone!

Categories