If file_exists Smarty - php

I'm new coding and Could someone tell me what i am doing wrong.
I want to see if a image exists in a folder but it won't display the image if it exists.
{assign var="backimg" value="/thumbs/backgrounds/movie_bg/`$mov.title|lower|replace:' ':'_'`.jpg"}
{ if file_exists($backimg) }
<div class="container_inner"
style="background:url(/thumbs/backgrounds/movie_bg/{$mov.title|lower|replace:' ':'_'}.jpg)no-repeat center;">
{else}
<div class="container_inner">
{/if}
Could someone tell me if there is anything wrong with my code.

Smarty is a templating language. You don't write PHP code into a template like that. You do the logic in the PHP code that calls the template, assigning whatever values are needed to render the page correctly to the template, and then render the template.
// In the PHP code.
// (Simplified. The actual code to initialize Smarty will usually be more complex.)
$smarty = new Smarty();
$smarty->assign("file_exists", file_exists('/file/path'));
$smarty->display('mytemplate.tpl');
// In the 'mytemplate.tpl' template file.
{if $file_exists}
<p>File exist!</p>
{else}
<p>File doesn't exist</p>
{/if}

Related

smarty how to replace exist char(php added) to judgment display image or display other column

have a database table include two column "brand_logo",and column "brand_name"
column "brand_logo" only put jpg file name(string) like
"logo.jpg"
column "brand_name" only put store name(string) like
"northwind_store_name"
i hope if not exist store logo image value("brand_logo")
then alter to only show store name
code like
php part
$tmp["Data"] = "data/brandlogo/" . $brand["brand_logo"];
smarty template part
<div>
{assign "bgImg" "{$brand.brand_logo|replace:'data/brandlogo/':''}.jpg"}
{if file_exists($bgImg)}
<img src="data/brandlogo/{$bgImg}" />
{else}
{$brand.brand_name}
{/if}
</div>
hope result (if brand_logo not empty)
<div>
<img src="data/brandlogo/logo.jpg" />
</div>
hope result (if brand_logo empty)
<div>
<p>northwind_store_name</p>
</div>
i'm not sure my smarty template part is correct or not correct
i already try to use simple if else smarty code like
<div>
{if $brand.brand_logo}
<img src="data/brandlogo/{$brand.brand_logo}" />
{else}
{$brand.brand_name}
{/if}
</div>
but it not work ($brand.brand_logo always not empty!! )
cause the "data/brandlogo/" need used to another php code so i can't remove
(mark:i'm not sure my smarty version so the smarty code maybe is have two write way style )
Don't check if the file exists in smarty, do it in php and then send the result (true or false) to smarty.

Smarty include tpl with dynamic content?

i have a little problem with smarty, but don't know how to fix this.
This is my index.tpl
<body>
{include file='header.tpl'}
{include file='leftmenu.tpl'}
<div id=content>
{include file="news.tpl" }
{include file="rightmenu.tpl"}
</body>
My problem there is this line
<div id=content>
{include file="news.tpl" }
news.php is a file which should display news.tpl , because it is my newssystem.
In news.php, i query the mysql database and give the result to news.tpl.
But when i write the above line, the news.tpl don't know where the result comes from.
Hope someone could help.
You do something like this, Smarty_Data will help you
$data = new Smarty_Data;
$data->assign('foo','bar');
$smarty->display('news.tpl',$data);
Sent your news array to assign function.
This will be a good alternative. You can simply add the PHP tag for including the PHP file
{php} include "news.php"; {/php}

What is this syntax and how to debug it?

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

User validation pattern in PHP with Smarty

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.

How to check a complex condition in Smarty(PHP)

I need to display a section or another in a smarty template. My condition is simple: if a smarty value starts with a string I should display one section, otherwise the other smarty section should be displayed. I can change only the tpl files.
{php}
if (substr($url,0,4) != 'http')
{
{/php}
section 1
{php}
}
else
{
{/php}
section 2
{php}
}
{/php}
The problem is that I can not read the url varible which was previously assigned using $smarty->assign. Basically, I'm looking for the smarty function that can be used to retrieve a value, or if there is a better solution.
First, I would clean up your code. You don't need php tags, you're using smarty:
{if substr($url,0,4) neq 'http'}
section 1
{else}
section 2
{/if}
That's untested but it should be pretty close..
Now, if you're trying to read something like a constant, for example a server variable like HTTP_HOST, you can do something like this:
{assign var='url' value=$smarty.server.HTTP_HOST}
{if substr($url,0,4) neq 'http'}
section 1
{else}
section 2
{/if}

Categories