PHP | Templates - Using files as template only - php

I'm currently using an own template system where I need to assign variables to their placeholders which will be replaced later. Unfortunately, It's not possible yet to use conditions like if/else directly inside the template. I always have to handle this in PHP and need to assign the final values to the template.
How it currently is in php:
$template->assign('{content}', ($b ? 'value' : 'value2')), and in html: <div>{content}</div>
What I want:
<div><?php echo ($b ? 'value' : 'value2'); ?></div> or <div><?php echo ($b ? 'value' : $this->renderAnotherTemplate()); ?></div>
I could use .php-files or something like that but as far as I remember, the template file name is visible in the url. So I need a way, to read a string from a template file, execute it's php and accessing the current variables I use in the parser (eval is dangerous).
Does anyone of you have a good idea, how to do this?
Thank you!

Why don’t you just include a PHP file?
include(‘template.php’)
You’ll be able to access all the variables inside your template file that are available in the scope where you call include(). You could wrap it within a method on your template object which may also accept variables to be used in your template file.
The other solution would be to pull in a templating language such as blade, twig or smarty. But honestly I’d just go with the simpler include on a small project

Related

PHP code in FatFree template

I am trying to work with FatFree framework and trying to use the template engine. I render the template with the following code -
echo Template::serve('template.php');
The problem which I'm facing is that, inside the template.php file the F3 tags are recognised but any PHP code doesn't work. For instance, if I have the following code in the template.php file -
<?php
if (F3::get('var') == 'var1') {
?>
<span>var1 is present</span>
<?php
} else {
?>
<span>var1 not present</span>
<?php
}
?>
Here both var1 is present and var1 not present is printed irrespective of the value of var. Also, php for loops are not working - so basically all the php code is not working.
However, if I used <F3:check> to write the above PHP code, then everything works fine. Can we not use PHP code in templates. If this is the case, this is a serious limitation.
I have found the answer, although I don't really like it.
There is two different functions, F3::render() and Template::serve()
With F3::render() you can evaluate PHP expressions and use the F3::get() to retrieve variables. According to the website: "The only issue with embedding PHP code in your templates is the conscious effort needed to stick to MVC principles"
The Template::serve() is for templating only. Meaning its simply to process the templating language.
So basically, and yes it sucks and doesn't make sense, you can evaluate PHP code in the F3::render() and you can't use templating variables ({{#var}}) -OR- you can use Template::serve() and you are limited to only calling PHP functions, and not truly evaluating PHP code.
Maybe try to use different template engine which will allow you define easier the blocks variable dependency?
For example in PHPTal http://phptal.org/manual/en/split/tal-condition.html you can do it like that:
<div tal:condition="php: var == 'var1'">
....
</div>
It is undocumented but you can put code within {~ ~} in a template and it will be converted to <?php ?> when the template is compiled (using v3.6).
e.g. {~ #color = 'red' ~} will become <?php $color = 'red' ?>

HTML treat code within brackets as PHP code

I am building my website completely in PHP. I am trying to make it as much flexible as possible.
I have seen there are some softwares made in PHP that are able to get a HTML page, and before showing it, the PHP code recognizes the code inside brackets {PHP Code} as PHP code, runs it and only then shows the final page.
<h1>Hi My Name is {echo $name}</h1>
How can I achieve the same? I know there is Smarty Code. But I do not want to learn Smarty, I just want to know how to check a HTML page with PHP, find every bracket and threat that as PHP before showing the page..?
Can you point me somewhere?
Are you looking for PHP's basic syntax?
If you enable short_open_tags (it usually is enabled by default), this will work:
<h1>Hi My Name is <?=$name?></h1>
otherwise, this will always work:
<h1>Hi My Name is <?php echo $name; ?></h1>
PHP is already a templating language - there often is no need to add another layer of templating on top of it.
I want to keep the template files separated from the php engine
In fact, you don't
Your template files would behave as native PHP files in every way.
So, there is asolutely no [logical] reason to prefer such a strange solution over native PHP.
use the php tags for the echo statement.
<h1>Hi my name is <?php echo $name; ?></h1>
Well, just point apache to index.php which includes phtml templates into itself. Use <?php ?> instead of { }.

How can I assign a smarty variable to a PHP variable

How can I assign a smarty variable to a PHP variable. I have
{assign var=arrayname value="/"|explode:$form.attributes}
{assign var=id value=$arrayname.2}
I want to assign the smarty variable id to a php variable
<?php
// I want $id here
?>
How can I do this?
Thanks in advance
I don't quite understand...how was your $arrayname passed to Smarty....? I assume you have used something like
<?php
$smarty->assign('arrayname',$arrayvariable)
?>
So in PHP there is equation:
$you_have_wanted==$arrayvariable[2]
This isn't really a situation you should find yourself in.
You should be able to either do what you want to do entirely in smarty or entirely in php.
If you explain in detail what your trying to do we can help you with one or the other
It is possible to use php in a smarty template but:
"This is for advanced users only, not normally needed and not recommended."
The whole point of the smarty template is to keep your business logic in a separate view.
Therefore you should deal with your variables in the php page then assign them to the template as in Lucifer Orichalcum's example.
If the user Id value you a trying to retrieve comes from a form you have to deal with the form values in the .php file ($_POST['id']) not in the .tpl
Same rule for your sessions variables.
Can you tell us more about where the Id comes from in the first place ?

Best way to store widgets, XTHML snippets and containers in PHP

I have a a few XHTML widgets and containers that have static or mostly static content. When I say mostly static, I mean a widget which is using one or more variables passed to it from a DB or a configuration. These are all used in more than one frontend page. Currently, I have stored them as PHP functions that return a XHTML string, and these functions are stored in a singular "functions" file that is "included" on every page. Where applicable, I either pass these functions configuration values or do some DB calls from within.
Is there another better way to store these in PHP code? Or is what I am doing pretty standard?
(I am not using Smarty or another template system and I'd rather not add another layer of abstraction.)
That's pretty standard, most you can do is separate them into files for each and then have a "includes" files that includes each one of them.
This way you would only have to load what you need per script / page.
(I am not using Smarty or another template system and I'd rather not add another layer of abstraction.)
I know this isnt what you want but this is exacly what id do. I wouldnt necessarily use a template engine with its wone grammar but i would use php and make seperate snippet templates containing the structural markup and then pass in what i need for example:
function get_partial($path, $args = array())
{
ob_start();
include($path);
return ob_get_clean();
}
function include_partial($path, $args = array())
{
echo get_partial($path, $args);
}
Then in your template:
<div class="<?php echo $args['classname']?>">
<h4><?php echo $args['title'] ?></h4>
<?php echo $args['content'] ?>
</div>

Zend organization question

So I had a question on general organization of code for the Zend framework with regard to the layout.
My layout is basically this:
(LAYOUT.PHTML)
<div id='header'>
<?= $this->Layout()->header ?>
</div>
<div id='main'>
<?= $this->Layout()->main ?>
</div>
<div id='footer'>
<?= $this->Layout()->footer ?>
</div>
and so on and so forth. Now, in order to keep my code in my header separate from the code of my main and the code of my footer, I've created a folder for my view that holds header.phtml, main.phtml, footer.phtml. I then use this code to assign the content of header.phtml into $this->layout()->header:
(INDEX.PHTML)
$this->Layout()->header = file_get_contents('index/header.phtml');
$this->Layout()->main = file_get_contents('index/main.phtml');
$this->Layout()->footer = file_get_contents('index/footer.phtml');
That was working great, but I've hit a point where I don't want main to be static HTML anymore. I would like to be able to insert some values with PHP. So in my Controller in indexAction, I want to be able to load from my database and put values into index/main.phtml. Is there a way to do this without restructuring my site?
If not is there a way to do it so that I can have:
The ability to put code into different sections of my layout, such as Layout()->header, Layout->footer.
Separate these pieces into different files, so that they're easy to find and organize, like my index/footer.phtml, index/main.phtml etc.
Not have to put that code into quotes unnecessarily to turn it into a string to pass it to Layout()->header etc.
Thank you guys so much for your help.
-Ethan
Here is an idea:
Assign layout()->header the filename instead of the contents.
Put your code in this file
In your layout file, include() or require() the layout->header().
Since your layout headers/footers are now parsed, you can use them just like a view.
The ->header in $this->layout()->header is response segment. You can render parts of response using $this->_helper->viewRenderer->setResponseSegment('header'); in an action.
If you use
$this->layout()->header = $this->render('index/header.phtml');
It will even use the view, therefore keeping all your variables defined when rendering the header.
I would suggest using something like
<?php echo ($header = $this->layout()->header)?
$header : $this->render('headerDefault.phtml'); ?>
in your layout file - it will render a default header from the layout folder if the view script doesn't override it.
Have you tried looking at view helpers. They are a way of structuring view logic into reusable and modular code. In this case you would use a view helper to generate each of your required segments. So your example view script would look like
$this->Layout()->header = $this->header();
$this->Layout()->main = $this->main();
$this->Layout()->footer = $this->footer();
The benefit of using view helpers over include and require statements is that all of the file handling and name resolution is handled by the framework. The manual has more information on how to set up the paths and usage examples etc.
helpers are good. Another option is like the above, putting filenames in header/footer - put the template names and use $this->render($this->layout()->header)), etc etc. This is just like the include/require above, but more consistent.

Categories