I use this code to create my form:
$form=$controller->beginWidget('CActiveForm', array(
'id'=>'my-form',
'enableAjaxValidation'=>false
));
When I do this, it prints the form. But I want to put this in a variable, because the entire form is returned as string in the method. How can I do this?
The following takes the output buffer, stores it into a variable and then erases it. Enjoy!!!
ob_start();
$form=$controller->beginWidget('CActiveForm', array(
'id'=>'my-form',
'enableAjaxValidation'=>false
));
$formToStore = ob_get_clean();
You can use the controller's renderPartial method to return a string. Simply put your entire form in a file by itself, let's call it _myForm.php.
Then use this line to write the form contents to the variable $form:
$form = $controller->renderPartial('//stuff/_myForm', $data, true);
The 3rd parameter set to true will return the contents of that renderPartial, instead of echoing. That includes 'overriding' the echoing of the widget itself.
The $data parameter includes any variables you need to pass to the form.
The file itself will need to be in the protected/views/stuff/ folder. The // start to the first parameter is a shortcut for the protected/views/ folder.
Alternately, use just '_myForm' for the first parameter if that file is in the same views folder as where the renderPartial is called from.
Note: this is not really functionally different than the option to use ob_start and ob_get_clean. It is simply the more Yii way to do it. There may be an advantage to one version over the other depending on the specifics of your situation.
Related
So I'm having quite a problem! I need to import a HTML file to a blade in Laravel, which I'm already doing! With this code:
<?php include ; ?>
I need to do it this way; for this project, I cannot transform the HTML files to blades.
But now comes a problem: my HTML files need to redirect me between them. For example if I have two HTML files, the first one needs to contain a redirect to the second one and vice versa. To do this, I need to pass a variable that contains that redirect to my HTML file: is that even possible?
I pass the collection that contains the argument to my blade already where I include the HTML File.
What I've tried:
I tried to pass the argument I need to the "href", imagine the argument is
$loop->iteration
Which it is, as I need to pass a int to my url, so I tried to :
href="{{$loop->iteration}}"
But it return me: exactly what I wrote in my url, so it doenst treat my argument as a php argument but as a string..
I am trying to implement a search function that will use ajax to query the database when a user types a character into the search box This will bring up any data within the database that begins with the character typed by the user.
I think this line, is your problem;
$this->load->view(array('words' => $wordlist));
You are trying to load an array of views.
The first parameter within the view function, is the name of the file you're trying to load. Like this;
$this->load->view('file_name', $data, true/false)
https://ellislab.com/codeigniter/user-guide/libraries/loader.html
If you want a specific type of data and simple out, you must use header response without $this->load->view just like this:
header('Content-type: application/json');
echo json_encode(array('words' => $wordlist));
Use that, you can easily get data with ajax
I have a PHP page that lists many products that each consist of HTML that -depending on the product- does or does not show certain information. I extracted this "product HTML" into a separate PHP file that I include many times. The external file uses variables for each of the product's attributes and also if statements so that parts of the HTML are not rendered at all when an attribute is not set.
The problem is that many of the attributes have default values that I do not want to set each time I call the include
So is there a mechanism in PHP that lets me include another PHP file in such a way that:
I can provide parameters to the file,
uses a default value for the parameters when I do not provide a value,
can be used multiple times in one file.
(This is the way <jsp:include works.)
Keep in mind that I cannot use simple variables (as far as I know) because they keep their value between two separate include calls.
When you include a file in PHP, that file has access to the current scope. So, we need a "clean" scope with no variables and populate the one we need. The best way to go is using a function and the PHP extract method:
function product($data){
extract($data);
require(....path to file);
}
Now call it:
product(array(
'foo' => 'bar',
....
));
This way, on the included file you got a $foo variable with 'bar' value and nothing else. This is how MVC frameworks deal with views.
Edit:
<?PHP
function product($data){
extract($data);
?>
<HTML content>
<?PHP
}
?>
Now "require_once" the file with this and call it like before.
I am a newbie coder trying to build a simple web app using PHP. I am trying to send an HTML email that has a variable that will change each time it is sent. The code to initiate the email is 'email.php' and contains:
$body = file_get_contents('welcome/green2.html.php');
Within the 'green2.html.php' file, I have a variable called $highlight that needs to be populated. The $highlight variable is defined within the 'email.php' file. I had tried to simply add within the 'green2.html.php' file, however it is not being parsed. I get a blank space where the variable should be when it is output.
Also, I have done an include 'welcome/green2.html.php' within the 'email.php' file. When I echo it, the $highlight var is shown on the resulting page, but not if I echo $body.
Any help would be much appreciated!
Have you tried the str_replace function? http://php.net/manual/en/function.str-replace.php.
Add a placeholder in HTML (for instance #name# for name, #email# for email), and then use the string replace function once you've loaded the content of the file.
$bodytag = str_replace("#name#", $name, $myfile);
Loading a file via file_get_contents() will not cause it to be parsed by PHP. It will simply be loaded as a static file, regardless of whether it contains PHP code or not.
If you want it to be parsed by PHP, you would need to include or require it.
But it sounds like you're trying to write a templating system for your emails. If this is what you're doing, you'd be better off not having it as PHP code to be parsed, but rather having placeholder markers in it, and then using str_replace() or similar functions to inject variables from your main program into the string.
Hope that helps.
Use http://php.net/manual/en/function.sprintf.php put a %s in your code instead of the variable read the content and put the string into the sprintf with the variable you want to put that's it. Hope this will help.
I am creating a custom form building system, which includes various tokens. These tokens are found using Regular Expressions, and depending on the type of toke, parsed. Some require simple replacement, some require cycles, and so forth.
Now I know, that RegExp is quite resource and time consuming, so I would like to be able to parse the code for the form once, creating a php code, and then save the PHP code, for next uses. How would I go about doing this?
So far I have only seen output caching. Is there a way to cache commands like echo and cycles like foreach()?
Because of misunderstandings, I'll create an example.
Unparsed template data:
Thank You for Your interest, [*Title*] [*Firstname*] [*Lastname*]. Here are the details of Your order!
[*KeyValuePairs*]
Here is the link to Your request: [*LinkToRequest*].
Parsed template:
"Thank You for Your interest, <?php echo $data->title;?> <?php echo $data->firstname;?> <?php echo $data->lastname;?>. Here are the details of Your order!
<?php foreach($data->values as $key=>$value){
echo $key."-".$value
}?>
Here is the link to Your request: <?php echo $data->linkToRequest;?>.
I would then save the parsed template, and instead of parsing the template every time, just pass the $data variable to the already parsed one, which would generate an output.
You simply generate the included file, you save it in a non-publicly accessible folder, and you include inside a PHP function using include($filename);
A code example:
function render( $___template, $___data_array = array() )
{
ob_start();
extract( $___data_array );
include ( $___template);
$output = ob_get_clean();
echo $output;
}
$data = array('Title' => 'My title', 'FirstName' => 'John');
render('templates/mytemplate.php', $data);
Note the key point is using extract ( http://php.net/extract ) to expand the array contents in real vars.
(inside the scope of the function $___data['FirstName'] becomes $FirstName)
UPDATE: this is, roughly, the method used by Wordpress, CodeIgniter and other frameworks to load their PHP based templates.
I'm not sure if understood your problem, but did you try using APC?
With APC you could cache variables so if you echo a specific variable, you could get it from cache.
You do all your calculations, save the information in some variables, and save those variables in the cache. Then, next time you just fetch that information from cache.
It's really easy to use APC. You just have to call apc_fetch($key) to fetch, and apc_store($key, $value, $howLongYouWant2Cache) to save it.
You best bet would to simply generate a PHP file and save it. I.e.,
$replacement = 'foobar';
$phpCodeTemplate = "<?php echo '$replacement'; ?>";
file_put_contents('some_unique_file_name.php', $phpCodeTemplate);
Just be very careful when dynamically generating PHP files, as you don't want to allow users to manipulate data to include anything malicious.
Then, in your process, simply check if the file exists, is so, run it, otherwise, generate the file.