Is this html code using php inside it without the <?php ?> signs? - php

{$smarty.section.page.index}
{/if}
{/section}
{if $pagesArray.is_next_page eq 'Y'}
<span class="resultsnext">
>
</span>
{/if}
I have written simple php based websites before but this is the first time I am getting involved in a complex php website that another person has already finished to a large extent. This is also the first time I'm seeing this type of code in a html template section of the website.
Basically, what I'm curious is, what are all the code in the {} curly brackets? Is it some sort of php code? Another php file refers this .html file that is containing the above code.
There is a lot of content being dynamically generated, but I've never seen '{}' curly brackets being used in a .html file while having it being used as part of a template so I was wondering if some of you guys could enlighten me.
thanks.

That is a PHP templating language, specifically, Smarty.
Smarty parses the file for its own syntax and replaces their placeholders with variables, etc that are bound to the smarty view.
Smarty Documentation.

That's smarty:
http://www.smarty.net/

You're looking at Smarty tags. Here's a link to the current documentation.

It's smarty :)
Actually you're looking at sections.
http://www.smarty.net/docsv2/en/language.function.section.tpl

Related

smarty processes strong (text) format over php -> put out modified php only

Have prepared a string modification with php in the classes file. Now in the smarty template (tpl) file when outputting the string I get a wrong text format. The project uses smarty 3.1-DEV and php 5.6.
I have this php code and assigned the variable in php class file:
$pattern = '/' . implode('|', $allergens_searchnames) . '/iu';
echo preg_replace_callback($pattern, function ($m) {
return mb_strtoupper($m[0]); }, $prodIngredients);
}
$this->assign("articleIngredients", $prodIngredients);
Outputting directly with "echo" over classes file I get on front end the desired format:
"Zutaten:
MARILLEn (75%), Zucker, Zitronensaft, Geliermittel: PEKTINe (aus Apfel)"
Implementation over smarty output is wrong:
"Zutaten:
Marillen (75%), Zucker, Zitronensaft, Geliermittel: Pektine (aus Apfel)"
Here like I implemented to the tpl-file:
<span class="pull-right">
{if isset($articleIngredients)}
<p>{$articleIngredients|unescape:'html'}</p>
{/if}
</span>
Due to the fact that I don't know much of the project and of smarty it is nearly impossible to fix this for me. Any help is appreciated.
I think one of these 2 might help you. Try using unescape:'htmlall' or Try printing the text as literal text
<span class="pull-right">
{if isset($articleIngredients)}
<p>{$articleIngredients}|unescape:'htmlall'}</p>
{/if}
source: https://www.smarty.net/docs/en/language.modifier.unescape.tpl
Anything within {literal}{/literal} tags is not interpreted, but displayed as-is.
<span class="pull-right">
{if isset($articleIngredients)}
<p>{literal}{$articleIngredients}{/literal}</p>
{/if}
</span>
Source: https://www.smarty.net/docs/en/language.function.literal.tpl
I found a solution to implement it directly on the template: smarty replace multiple values
problem is to hardcode all diffrent possibilities to replace into two arrays. Maybe I can find another solution for this.

Echo'ing a "{FORUM_NAME}" and Ignoring the "{}"

I'm looking for something that Is really hard for me to do.. I really tried to search all over the net for Solution, But I couldn't seem to find any. I also tried doing this for hours.
What I'm doing: Making a theme for PHPBB2, Installed a MOD that can include PHP in themes.
What is the problem: When I'm doing {} tags in php, It just can't echo those tags.
Let's say I have a function that creates a Table for me, like that:
CreateMyTable(Name,Size,Color);
I put in the function those strings:
CreateMyTable("{FORUM_NAME}",1000,red);
The title stays blank, I actually want it to echo {FORUM_NAME}.
How can I do this?
P.S: I can't do this
CreateMyTable(?>{FORUM_NAME}<?php , 1000, red);
It's not going to work becuase <? = <!-- PHP --> , ?> = <!-- ENDPHP -->.
Thanks for your help :)
If you look in the PHPbb2 template class, you'll find that the template is simply an evaluated set of PHP using the eval() function. You can either print the contents of the PHP before it is parsed using eval() and then use the variable name that the template gives, IE something like (which may not work depending how your template is setup):
CreateMyTable(((isset($this->_tpldata['.'][0]['FORUM_NAME'])) ? $this->_tpldata['.'][0]['FORUM_NAME'] : '' ),1000,randomcolor());
Please note, in order to do it similar to the way above you'd actually have to insert this into your template class.
An much better solution is to avoid using the mod that allows PHP in templates and use JavaScript in the templates to create the function, then print a call to that JavaScript function.
This will work:
CreateMyTable(FORUM_NAME,1000,red);
I also noticed that red is used without quotes - is this also a constant? If it's a variable it needs to have a $ in front of it. If it's a string it should be between quotes.
CreateMyTable(FORUM_NAME,1000,"red");

Filtering data in loop Smarty

Is there any way to use preg_match or any other similar smarty function to do the following thing:
{foreach $resultsdata as $resultsData}
<div class="site_text">
{$resultsData.text|substr:0:500}
</div>
{/foreach}
I want to filter $resultsData.text and display only one part of this text.
I'm asking because there's a plugin you may use. But it's only for Smarty 3, as I see.
By the way, from your code it seems preg_replace would be a better solution, and it's already internal function - regex_replace - for it.
You are taking idea of templates wrong.
Template should be used only to display data.
But data itself have to be prepared in the business logic part.
Otherwise there will be no good from templates at all.

different ways of code writing. need suggestion

My friend writes everything using phps echo.
I mean he starts <? and echo everything, including header, main part, footer, he even has style.php file where he echos some css and then includes it in main project.
question is why is it better to do that way ? Or is it better at all?
Cause in dreamveawer everything is red. I mean it understands evrything as phps srtings and makes all text red.
If you do not use echo you have different colors in code and you can see where is ccs, javascript or html.
So what's better to write normally or I should try the same "echo everything" practice ?
No, it is no better to do that way.
Do not use echo to output HTML but divide your code into 2 parts - business logic part and presentation logic part, a latter one consists of mostly HTML with PHP used only to output data coming from the business logic part
a little example of such a template:
<? if ($err): ?>
<? foreach($err as $e): ?>
<div class="err"><?=$e?></div>
<? endforeach ?>
<? endif ?>
<form>
<input type="text" name="name" value="<?=$form['name']?>">
<textarea name="comments"><?=$form['comments']?></textarea>
<input type="submit">
</form>
If that's how he does it, then please slap him across the back of the head for us please?
But seriously, there is this thing called MVC, please take a look at it, comprehend it, implement it and explain it to your friend.
That's horrendous.
You should try not to mix PHP and HTML if you can help it.
In some cases you will be interleaving some dynamic content with static content (and you could use a templating engine for that); but outputting the entire HTML document via PHP statements is a clear sign of insanity and sadism.
You might be interested in templates. They're not html nor php, but a combination between these two.
The advantage is that you can see/edit/update/maintain your code much easier, because the actual php part is somewhat separated from the html markup.
The main disadvantage is that it might be a bit slower, since php reads the template, makes replacements and then spits out the html.
Here's a link to some of the most used php template engines.
Using HTML code instead of echoing has the advantage that the editor (Dreamweaver in your case) can do syntax highlighting. This will help you find errors in your HTML faster.
Also, you don't have to think too much about escaping quotes in your HTML (you still need to think about proper escaping your PHP variables anyway).
I can think of several ways the bad habit of echoing everything can be formed:
Errors from missing PHP tags while intermingling PHP and HTML. For a newbie it takes a while to wrap your head around the concept of using one language (PHP) to write code in another language (HTML). I remember when I first learned PHP I thought it would be easier to echo everything instead of opening and closing PHP tags all the time.
Coming from a language background where every output must be printed explicitly.
Having read some insane micro-optimization article on the web that claims echoing is faster or more secure.

Smarty Object Property Inside Its PHP Tag

I have an smarty application where a series of addresses are being printed. It has a zip code too so at some point in template I am using.
{foreach item=i from=$members}
{$i.ZIP}
{/foreach}
The above code works though I am strictly making zip codes to 5 digits which I know can be accomplished by the following code.
{foreach item=i from=$members}
substr_replace("00000", {$i.ZIP}, 5 - strlen({$i.ZIP}));
{/foreach}
But the above code doesn't work and gives run time error. Is there something I am missing?
You can't use PHP code in a Smarty template unless you wrap it in {php} tags. In this case, you can avoid that by using string_format.
I think this should do:
{$i.ZIP|string_format:"%05s"}

Categories