I'm using PlatesPHP on a very basic level with static data rather than extracting it from db.
In my controller I have:
<?php
require 'vendor/autoload.pkp';
echo $templates->render('index', [
'project_1_checklist_point_1_help' => 'Google',
]);
and then in the index.php the following
<p><?=$this->e($project_1_checklist_point_1_help)?></p>
and in the template.php standard html skeleton.
It shows up as
Google
rather than a link, which I want.
Google
I've tried htmlentity() and htmlspecialchars(), but they are not what I was looking for at all.
Any ideas?
Cheers!
If you want the un-escaped string, just echo the variable without the method $this->e():
<p><?= $project_1_checklist_point_1_help ?></p>
The method $this->e() (short for $this->escape()) is equal to htmlspecialchars(), which html-encodes the string.
You can read more in depth about it the manual: http://platesphp.com/templates/escaping/
Related
When using Laravel, you can use { $variable } instead of <?php echo $variable; ?>, can you do this when you are using PHP without Laravel?
Let me see if I got you right. You want to put variable elements inside your HTML template files without using the ugly "<?php echo ...; ?>" ?
What you need is a Template engine. You could get one of these ready-to-use template system and learn how to use them (but they don't have the exact behaviour you describe) or you could code your own parser.
I personnally made my own HTML parser, its behaviour is adapted to the use I make of it. For what you need, all you need is to read HTML files and replace using a regexp
^\{ (.+) \}$
with
$($1)
I have made 2 views: 'list' and 'maps'
The 'list' view has an exposed filter input field (called title)
I try made a link in the head-section of the list-view (text-format: PHP-code) to the maps-view.
My question is, how can I do this with the GET method?
This code don't work:
maps
That's easy :) you missed the echo:
maps
# ^^^^
edit: there is also a <?= opening tag (instead of <? and <?php) which automatically calls echo (e.g. <?= $_GET['title'] ?>) but some people find them bad. Especially that modern frameworks such as Symfony2 recommend to disable short_open_tags.
I'm attempting to make a template file for a CMS that I'm making where the template file can contain variables like {username} as regular text that get replaced when the page gets included on the index.php page.
Example:
Index Page:
<?php include('templates/123/index.php'); ?>
templates/123/index.php page
<?php include('header.php'); ?>
Welcome {username}
<?php include('footer.php'); ?>
I've tried several methods; however, always run into problems because the page I'm trying to change the content on includes PHP code. Every method I try either 1) messes up because the opening and closing of PHP tags within the document OR 2) just echoes out the PHP code in the document. Is there any way that I can still achieve this? Maybe even with a class of some kind? I just want to be able to achieve this safely.
I will also be using this to where custom variables like {content1} get replaces with a php code that will be ioncubed that retrieves the data from database for content located in column1, same with {column2} {column3} and {column4}. I'm just trying to make the creation of templates extremely easy. (so I'd like to make the code work for that as well)
My preferred method of doing stuff like this involves starting my code with:
ob_start(function($c) {
$replacements = array(
"username"=>"Kolink",
"rank"=>"Awesome"
);
return preg_replace_callback("/{(\w+)}/",function($m) use ($replacements) {
return isset($replacements[$m[1]]) ? $replacements[$m[1]] : $m[0];
},$c);
});
Two steps I suggest
Load the result of your file "templates/123/index.php" into a variable. see this link for how to do it assign output of execution of PHP script to a variable?
use strtr() function to replace your placeholder i.e {username} with actual values
I think this will server your needs.
Is there a way to print field content without getting all the markup? I'm new to Drupal, but I'm aware of the field.tpl.php, however, I'm just wondering if there's a quicker way to get the content in a node--custom.tpl.php. It would compare to Wordpress's <?php echo get_field('field_name'); ?>
Well, apart from using field.tpl.php, I can think of 2 solutions:
first:
Use a php snippet to strip html tags in your template.php.
in your template.php
function mytheme_strip_html_tags($n_field) {
return preg_replace("/<.*?>/", "", $n_field);
}
then call the function mytheme_strip_html_tags($field_name)
if you use several themes, however, you need to copy this snippet to each one of them.
EDIT: You can make a module and place that snippet inside. This way it works with every theme.
second:
Download the tokens module. Tokens are references to your fields. Tokens module have a output mode that strips html for you. [field_name-raw]
You need to follow instructions in how to add tokens, but is not that difficult.
You have access to the $node variable inside a node.tpl.php, so:
<?php print $node->field_monkey_height; ?>
should work... note that many fields will hide their data inside arrays (for multiple value fields, etc) so you may need to do a bit of:
<?php drupal_set_message(print_r($node->field_monkey_height), 1); ?>
...to figure out the exact path to the data you need.
You simply can use PHP's strip_tags() like so:
<?php print strip_tags($node->field_name[LANGUAGE_NONE][0]['value']); ?>
I was wondering, is there any easy way to have the html generated by codeigniter be more structured and more readable? For example, if I do this:
echo form_open('do/send',array("id" => "contact_form"));
echo form_label("Name:", "name");
echo form_input(array("name" => "name"));
echo form_submit("submit", "Submit");
echo form_close();
The html generated will be in one single line and no structure.
I could do something like this:
echo form_submit("submit", "Submit")."\n";
But that's not really practical.
Any ideas?
Reading through CodeIgniter Form Creating Library it seems that your options are either manually putting in the \n, like you suggested or put it in the html mockup and space it out that way (for this see the referenced page under: Open "books_input.php" within CodeIgniter\system\application\views. Update like following code:)
You should try Dan Horrigan's Formation library. It's much nicer than CodeIgniter's helper output, and easier to use, also.