I am currently working on moving an expression engine site from one server to another and i noticed one issue i am having a hardtime debugging. When i upload an logo image all seems fine but the index.php page that the logo is displayed on it has this code
{embed="shared/head"}
<body class="{if segment_1 == ''}home{if:else}{segment_1}{/if}">
<div id="page" class="container">
<div class="span-22 prepend-1 append-1 last">
{embed="shared/masthead"}
{if logo !=''}
<div class="news_item_logo">
{organization}
{if link}<img src="{logo}" width="130" alt="{title}" />{if:else}
<img src="{logo}" width="130" alt="{title}" />{/if}
{/organization}
</div><!-- /.news_item_logo -->
<ul>
<li><h3>{title}</h3></li>
<li>{pub_date}</li>
{organization}
<li>{if link}{/if}{exp:php_text_format type="lowercase"}{if url_text != ''}{url_text}{if:else}{name}{/if}{if link}{/exp:php_text_format}{/if}</li>
{/organization}
<li>{if file}PDF{/if}{if web_link !='' AND file !=''} | {/if}{if web_link}HTML{/if}</li>
</ul>
{if:else}
<ul class="no_logo">
<li><h3>{title}</h3></li>
My question is this, I see curly brackets {} around if statements and i want to know first what language it is and second is there a way to debug like php print_r() because the code always goes to the else with the no_logo class and i want to know what and how i can test these variables "segment1" and "logo" and "organization" and "url" How do and where do i inspect these variables
You can gain some info about the given variables and values in the template using the following within your index.php:
<?php
$EE = get_instance();
var_dump($this->EE->TMPL);
?>
Note that PHP must be enabled in templates for that to work (see PHP in Templates).
{embed="shared/head"} - include the template head from the template group shared
<body class="{if segment_1 == ''}home{if:else}{segment_1}{/if}">
if the URI segment (EE/CI works with segments eg site.com/segment1/segment2/xxx) is empty (you are on the home page (www.site.com), then add no body class.
else, the user is on a page (in EE this is a template group), so set the class to be the name of the template group.
site.com/about-us produces class="about-us" - handy for page specific styling.
{embed="shared/masthead"} - include masthead
and so on.
The rest are conditionals to check if the variables have values, and outputs them
I presume you're using EE2.0, I'm not sure what {organizaton} is specifically, but that style:
{organization} {foo} {/organization}
in code igniter at least, is generally the equivalent of a foreach or looping through a recordset:
foreach($organizations as $organization) { // do something }
This is written in Expression Engine's own templating language.
You would have to check the documentation to see whether there is any way to debug variables.
Possibly helpful links:
Quick Reference Chart
PHP in Templates
Related
I'm trying to use php-cs-fixer with a WordPress project, which means I (unfortunately) have files with a mix of PHP and HTML. I'm using the #PSR12 ruleset.
I'm having trouble with getting HTML within PHP control structures to indent correctly. Take this example snippet:
<?php if (!empty($related_posts)) : ?>
<div class="module--related_posts alignfull has-2-columns has-hover-state slider-on-mobile">
<h3 class="has-text-align-center">Related <?= esc_html($title) ?></h3>
</div>
<?php endif ?>
php-cs-fixer reformats it to:
<?php if (!empty($related_posts)) : ?>
<div class="module--related_posts alignfull has-2-columns has-hover-state slider-on-mobile">
<h3 class="has-text-align-center">Related <?= esc_html($title) ?>
</h3>
</div>
<?php endif ?>
Note the closing h3 tag has been moved to a new line, and the first-level indent within the if statement body has been removed.
The h3 issue I can live with, as this is resolved if I put the opening tag on its own line:
<h3 class="has-text-align-center">
Related <?= esc_html($title) ?>
</h3>
...but the lack of indent within the if statement is going to do my head in. The same thing happens with for and while statements.
Is there a rule in php-cs-fixer that I've overlooked that will resolve this?
The Answer of keradus on the "PHP code does not align with html code" issue:
PHP CS Fixer was never written with supporting mixed html/php file.
Some fixers are supporting one php part and one html part inside single file, but not big mix of them, like in template files.
If we would like to support template files, we would need to not only detect and track concrete fixers, but also provide some big integration test (like we do for Sf ruleset) that it remains to work for most important rules.
Before that happen, I would not claim that we officially support html/php mixed-files.
And in another answer:
we do not aim to fix mixed file (PHP and HTML in single file)
Not really an answer but I ended up just switching to PHP_CodeSniffer to get around this.
I'd like to remove some elements such as <footer> tag, menu row etc. on pages where body has unique classes like: 'shop_news_list' and 'shop_news'. The site is based on Smarty. I've tried to put those elements I want to delete in between something like this:
{if !($body_class == 'shop_news_list' || $body_class == 'shop_news')}
<footer>
<div class="innerfooter container row">
...
</div>
</footer>
{/if}
However it doesn't work or makes an element disappear from every page. Is there any other way I can do this or maybe you know how can I properly check if body has specific class. (I can not create new templates to use them on different pages).
A certain blogsite allow its users to fully customize the entire html page of their blogg, change doctype and all, and they use tags for where they want the user to dipslay things like titlename and amount of comments etc.
i.e this is code you can edit and submit under blog design settings:
<tag:archivelist>
<li>${ArchiveName}</li>
</tag:archivelist>
</ul>
<tag:if test="${hasLinks == 'true'}">
<div class="navheader">Links</div>
<ul>
<tag:linklist>
<li>${LinkName}</li>
</tag:linklist>
</ul>
</tag:if>
<tag:if test="${hasBooks == 'true'}">
<div class="navheader">Books</div>
<ul>
<tag:booklist>
<li><img class="thumbnail" src="${BookImageSmall}" border="0" alt="${BookTitle}" title="${BookTitle}" /><br />${BookTitle}</li>
</tag:booklist>
Now, how do they replace the ${ArchiveLink} part to make it into something from mysql table, how do they extract and replace what method? preg_replace ?
They are most likely using XLST (or a more elaborate custom templating engine that makes use of it) to transform the markup into standard HTML. Though i think the variables they are using are different... if i recall XSL variables are {$VariableName} as opposed to ${VariableName}.
I want to keep my templates tidy and nicely intented but would like to deliver only very compact HTML to the browser.
Missing a better idea, I was wondering if there is something wrong with wrapping whole Smarty templates in {strip} tags like so?
{strip}
<div class="albums">
<h2>Alle Foto-Alben</h2>
<ul class="photos clearfix">
{foreach $albums as $album}
{if $album->publishedPhotos|count > 0}
<li>
<div>
<a href="album.php?id={$album->id}">
<img src="{$album->titlepic->thumb}" alt="{$album->titlepic->title}" />
<span class="title">{$album->title}</span>
<span class="counter">{$album->publishedPhotos|count} Foto{if $album->publishedPhotos|count != 1}s{/if}</span>
</a>
</div>
</li>
{/if}
{/foreach}
</ul>
</div>
{/strip}
It smells a bit unprofessional to me but I could not come up with something better.
One downside definitely is that you have to wrap every single one of your templates in those tags.
I'm happy to be corrected and would love to hear different approaches to keeping delivered code compact.
While it's not wrong i would suggest using a pre-filter instead. A pre-filter only runs when the template is compiling (so it doesn't slow down the server) and you don't need to wrap every template in {strip}. The following rows of code is taken from a project I am working on. It effectively strips most whitespace.
/* Minify the html */
function smarty_pre_minify($tpl_source, $smarty) { return preg_replace('/[ \t\n\r]+/s', ' ', $tpl_source); }
$smarty->registerFilter('pre', 'smarty_pre_minify');
Nope, there's nothing wrong with this.
Do it in a header/footer pair of templates, rather than wrapping each page individually.
Alternately, you could trigger the stripping on the PHP side of things.
Notice: Striptags will ignore includes. Free tip of the day. :-)
I am using PHP and Smarty. I have a simple application that:
Shows page header
Checks if user is logged in -> check session
Checks if user wants to log out -> then clear session
If logged in shows a menu.
If not logged in, then challenges user for id/password -> set session
Shows page footer
Now I want to add the following to header:
username if logged in
"login" string if not logged in
To do this seems to require the placement of my PrintHeader function to become complicated.
I now cannot print header, until I know if user is logged in/just logged in/has logged out,
plus header has to go before user/password challenge.
What approach can I use here?
Is it possible to buffer a "not logged in" header, then replace it with "logged if header"
if required.
SmartyApp::PrintHeader();
Application->IsLoggedIn()
Application->DoesUserWantsToLogOut()
Application->IfNotLoggedInThenShowChallengeToUser()
Application->IfLoggedInThenShowFullMenu()
SmartyApp::PrintFooter();
I think a more convenient pattern would be to incorporate conditions within the templates themselves, rather than keeping multiple templates for each possible outcome. For example:
$smarty->assign('user', new User($_SESSION));
$smarty->display('index.tpl');
<!-- header.tpl -->
{if $user->isLogged()}
<p>Welcome {$username}</p>
{else}
<p>Please Log In</p>
{/if}
<!-- body.tpl -->
{if $user->isLogged()}
{include file="menu.tpl"}
{else}
<p>You must be signed in to view this menu</p>
{/if}
<!-- index.tpl -->
{include file="header.tpl"}
{include file="body.tpl"}
{include file="footer.tpl"}
The point of Smarty is that it separates logic from design of your page. In your case your logic is now controlling your design and that is not what Smarty is designed to do. If you prefer to control the design from within the application then you shouldn't use Smarty.
If you do want to use Smarty then remove your PrintHeader() and PrintFooter() then inside your Smarty template you put the design related to the display.
Probably the easiest way to do it following your pattern would be for the application to fetch the appropriate template and assign the appropriate variables.
Application->IsLoggedIn(); // assign some login variables
Application->DoesUserWantsToLogOut(); // probably redirect and logout and never get to display
Application->IfNotLoggedInThenShowChallengeToUser(); // template would be the login template
Application->IfLoggedInThenShowFullMenu(); // fetch and assign a menu to the template
$_smarty->display( Application->template );
Then in each smarty template:
{include file=header.tpl }
HTML for each template
{include file=footer.tpl }
Note that you can assign variables to the header and footer includes just like you would the regular template. So if you have a variable {$username} in the header. You just assign it in your Application menu output and it will set. Something like this is probably what you want: {if $username} {$username} {else} Log-In{/if}
Now just as opinion I would design it somewhat differently. I don't think OO means "don't use if statements". I think something like what i have below is clearer:
$app = new App();
if( $app->loggedIn() ) {
if( $app->logout() ) {
$app->redirect('goodbye.php');
}
$smarty->assign( 'menu', $app->menu() );
$smarty->assign( 'user', $app->user() );
$template = 'main.tpl';
}
else {
$template = 'login.tpl';
}
$smarty->display($template);
The smarty templates would look the same as the example I gave above.