How to include static html in php/smarty - php

I have been asked to include some html snippet in this php/smarty page. It's basically a sales agreement at the end of an overview page before you pay.
What is the code to include static html into my php/smarty template?
Thanks...

Do you mean include a file containing static HTML?
{include file="htmlfile.html"}
edit
As the file is just HTML, it might be better to use {fetch}, e.g.
{fetch file="path/to/html/file.html"}
For local files, either a full system
file path must be given, or a path
relative to the executed php script.

Like any other templates, smarty files are HTML files itself. you can just paste your HTML into smarty template.

If you are trying to include some code containing javascript or similar, you can use
{literal}{/literal} tags
to inlude a file you can use {include file="htmlfile.html"} as Tom said.

Before you display the template, in the PHP file try this:
$smarty->assign('Sectionfile','section-name.html');
$smarty->display('template.tpl');
in the template itself:
{include file="html_dir/$Sectionfile"}

Related

How to retrieve content in layout zend framework2?

I was using phtml files in zend framework. Now I am using .tpl files.
I found how to use html script and all. But when I want to use php code. Then I'm using:
<?php
echo "test";
echo $this->content;
?>
The problem with this is it is in layout.tpl file. Main content is in index.tpl of other module.
Rather than fetching the content of index file It echoing just 'test'.How to make it works?
Edited: I also tried {$this->content}.
If you are using the Smarty Templating Engine and the SmartyModule, then you will have to use Smarty syntax in your view scripts, since the Zend\View\Renderer\PhpRenderer will be overridden by the Smarty Renderer (and the Smarty Templating Engine). Also, if you wish to use layouts with Smarty, please see Smarty's Template Inheritance mechanism. Here is an example:
layout.tpl
<html>
<head>
<title>{block name=title}Default Page Title{/block}</title>
</head>
<body>
{block name=body}{/block}
</body>
</html>
mypage.tpl
{extends file="layout.tpl"}
{block name=title}My Page Title{/block}
{block name=body}My HTML Page Body goes here{/block}
Otherwise, if you are using the PhpRenderer, it will not "recognize" any templating language, even if you change the view script's file extension to .tpl, since it will simply include the content of the view scripts (see lines 502-503 of the renderer's source code). Therefore, as with any include, the PHP code will be executed immediately and stored in the renderer's $__content property. This is probably the reason why your echo command is immediately executed.
So, basically, you will have to choose your renderer (PhpRenderer or Smarty Renderer through the SmartyModule) and then abide by its inner workings (PHP/HTML or Smarty syntax (ex. variables), respectively).

smarty include markdown file

I want to include a markdown file in my Smarty template.
In PHP I have this:
$smarty->assign('text', 'PATH_TO_FILE/text.md');
in the template I use:
{include file=$text}
but this returns an unformated block of text.
I tried using David Scherer’s markdown plugin for Smarty:
{markdown text=$text}
but this just returns the file path stored in the $text variable.
How do I get Smarty to parse an included file as markdown?
You could instead read the file and assign the contents to the variable, then use the markdown plugin.
$smarty->assign('text', file_get_contents('PATH_TO_FILE/text.md'));
Manual page for file_get_contents()

Including PHP file inside smarty template file

I have completely no idea about smarty template system. What I am trying to do is include a php file to get some variable inside a .tpl file (this is a WHMCS template).
I have tried like:
{php} include ('file.php'); {/php} //doesn't work
{include_php file='file.php'} //doesn't work
I have also followed the answer of this question. Still couldn't get it working.
How can I include code.php inside header.tpl of WHMCS? Any help please?
Just for reference: both tpl and php file is in the same directory, if that helps anyway.
It's really not recommended to use php code in Smarty. In fact it's now deprecated and you should avoid such solution as much as possible because it often doesn't have sense.
However if you really want to use PHP in your Smarty files for some reason you need to use SmartyBC (Smarty Backward Compatibility) class instead of Smarty class.
So for example instead of:
require_once(_PS_SMARTY_DIR_.'Smarty.class.php');
$smarty = new Smarty();
you should use:
require_once('SmartyBC.class.php');
$smarty = new SmartyBC();
Then you will be able to use PHP in your Smarty template files
EDIT
If you have just problem with including it's probably your directories problem (however you haven't showed any errors).
I assume you have your files inside your templates directory and you set it properly using:
$smarty->setTemplateDir('templates');
if you simple display index.tpl file in Smarty and you have this PHP file in the same directory (in template directory) you can include it without a path.
However if you include in this index.tpl file another tpl file, when you want to include php file you need to pass the full path to this PHP file, for example:
{include_php 'templates/file.php''}
Your using Smarty the wrong way. The whole point of Smarty is to NOT include any PHP in your presentation bits (views, a.k.a. the good ol' HTML).
So, whatever you're trying to do in that PHP file, just let it do its magic, but send the actual result to Smarty. For example, do you want to show a table of users you get out of a database? Execute the query, fetch the result and pass the results (like an array of results) to smarty like this:
<?php
$smarty = new Smarty();
$users = $db->query('SELECT * FROM users');
// Assign query results to template file.
$smarty->assign('users', $users);
// Compile and display the template.
$smarty->display('header.tpl');
Now, in your smarty template you can access that array like this:
<html>
{foreach from=$users item=user}
Username: {$user->username}<br />
{/foreach}
</html>
I hope you see where I am going. Keep your application logic in the PHP file, and let the template just take care of the looks. Keep the template as dumb as possible!
You get data into Smarty by assigning it. Embedding PHP is not recommended, and deprecated from Smarty 3.
php:
$smarty->assign('foo','bar');
smarty:
{$foo}

Can PHP be inserted into a javascript call in my header?

I have a wordpress theme that I like to duplicate. To make things easier on myself, I'm trying to use the bloginfo function to call my jquery file..like this:
<script type="text/javascript" src="<?php echo bloginfo("template_url"); ?>/js/jquery-1.4.2.min.js"></script>
I just can't get it to work. When I check my source file it looks exactly like the code above.
Can this even be done, or should I just forget it? Thanks guys!
Are you sure the above code is actually in a PHP-file and gets parsed by the server? I can't think of a different reason why PHP-code should just be printed and not executed.
See the Referencing Files From a Template chapter in http://codex.wordpress.org/Theme_Development
Whats the name of this file? It should end in .php.
What does the source look like when you view it from the browser?
Are there other places in the file where you use and is that data correct?
Make sure there are no blank lines at the end of the source file. PHP doesn't like blanks lines at the end of the code.
Two problems: No need for the echo in the WP template tag; should be
<?php bloginfo("template_url"); ?>
Template Tags/info « WordPress Codex
And, why are you trying to include jQuery from the template directory, when it resides in wp-includes/js? You should be using Function Reference/wp enqueue script, if your theme doesn't already include jQuery.
You should try to edit your theme's header.php file directly because custom fields of your theme may not be executed by PHP interpreter.
As I've never used PHP, this is only a guess...
PHP is a server side language, and you're tyring to use it on the client side?

Get Template Name - Smarty

I'm dealing with website is a mess and need to find out the template file that smarty is rendering.
Is there a method on the smarty template object that I can call to get the current template file?
for example
echo $tplObj->getTemplate(); // echos "shop/templates/cart.tpl"
From the doc:
{$smarty.template}
Returns the name of the current template being processed. The following example shows the container.tpl and the included banner.tpl with {$smarty.template} within both.
<b>Main container is {$smarty.template}</b>
{include file='banner.tpl'}
will output
<b>Main page is container.tpl</b>
banner.tpl
Maybe the {debug} tag and its associated Debugging console could help, here ?

Categories