smarty include markdown file - php

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()

Related

PHP Smarty template main page problam

I want to change my website main page, using php code!, I found the tpl file which I need to change, problem is that I have no experience in tpl and I want to write it with php but it won't work. I am trying to use:
{include_php file="/path/to/somefile.php"}
To use php language but it does not work. How does it work with tpl in Smarty templates using php language.
My suggestion the following is a php file
include_once("/var/www/html/smarty/libs/Smarty.class.php");
$smartyobject=new Smarty();
$varr="stackoverflow"; //php variable it can also be a array";
$smartyobject->assign("website", $varr); //assigning a variable to "website", and this is now a smarty variable.
$smartyobject->display("tplfile"); //this is the actuall file that get displayed, in this tpl file you can use "website" variable {$website}
smarty has so many in functions loops.. so there is no need include any php file. do whatever you want to in the php file save the result in a variable and display it in a tpl file.

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}

smarty fetch php file not working

I want to fetch a php file in my .tpl but it will be display as plain text. if i use include it works, but need to fetch it.
{fetch file="http://domain.xy/index.php?c=2"}
thx for help
{php}
//include php code to get the text file here
{/php}
You could maybe do something like this and include the php code you need between the tags. The php tags let you put straight php into the template.
Or it may be better to fetch the file content before you load the template and assign it to a smarty variable then call the template. Something like this:
$smarty->assign('some_var', file_get_contents ('http://domain.xy/index.php?c=2'));
$smarty->display('the_current.tpl');
thx. the following code works for me
{php}
$url = file_get_contents('http://domain.xy/index.php?c=1');
echo $url;
{/php}

How to include static html in php/smarty

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"}

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