Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Fetch different resources

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
GAngZta
Smarty n00b


Joined: 25 Jul 2016
Posts: 2

PostPosted: Mon Jul 25, 2016 3:58 pm    Post subject: Fetch different resources Reply with quote

Hello!
I need your help for the following problem:

I have to fetch and render x templates. This is my solution:
Code:
$smarty->fetch('extends:template1.tpl|template2.tpl|template3.tpl|...');

This works.

But in our system we also need to add x template strings.
I tried different ways. Like so
Code:
$sTpl1 = file_get_contents('BIG_FAT_TPL.tpl');
$sTpl2 = file_get_contents('BIG_FAT_TPL2.tpl');

$smarty->fetch('extends:template1.tpl|template2.tpl|...|string:' . $sTpl1 . '|' . $sTpl2);

But everytime I get a smarty exception but I really don't know whats wrong.

When I load $sTpl1 and $sTpl2 not as string but as resource, all is ok.

Can someone tell me, how I have to build the string inside $smarty->fetch(); to load multiple tpl files AND tpl strings?
Thank you!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1615

PostPosted: Tue Jul 26, 2016 10:45 am    Post subject: Reply with quote

http://www.smarty.net/docs/en/resources.extends.tpl
Either your Smarty is outdated, or your string templates are not encoded properly.
Back to top
View user's profile Send private message
GAngZta
Smarty n00b


Joined: 25 Jul 2016
Posts: 2

PostPosted: Wed Jul 27, 2016 5:22 am    Post subject: Reply with quote

Thanks for your reply.
I know this doc but it can't help me because it do not show how to use multiple template strings.

I also tried strings base64 encoded.
Code:
fetch('extends:template1.tpl|template2.tpl|string:base64:BASE64STRING1|string:base64:BASE64STRING2)


But also with this method I get an exception when I use it together with template files in fetch()

Can you send me an example as you would do this?
Back to top
View user's profile Send private message
Aristophan
Smarty Regular


Joined: 10 Jan 2011
Posts: 96

PostPosted: Wed Jul 27, 2016 8:31 am    Post subject: Reply with quote

Hi
You wont get much and real help from this user. He is spamming this forum with lots of useless and unhelpful and unfriendly posts, trying to take over.

From the docs, it is:
Code:
$smarty->assign('foo','value');
$template_string = 'display {$foo} here';
$smarty->display('string:'.$template_string); // compiles for later reuse
$smarty->display('eval:'.$template_string); // compiles every time

in your case probably something like this:
Code:
$smarty->fetch('extends:template1.tpl|template2.tpl);
$smarty->display('string:'.$sTpl1);
$smarty->display('string:'.$sTpl2);

or you may try with this:
Code:
$smarty->fetch('extends:template1.tpl|template2.tpl|eval:{$sTpl1}|eval:{$sTpl2});

Maybe that works.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1615

PostPosted: Wed Jul 27, 2016 12:09 pm    Post subject: Reply with quote

Aristophan wrote:
Hi
You wont get much and real help from this user. He is spamming this forum with lots of useless and unhelpful and unfriendly posts, trying to take over.

Maybe that works.

Do note: you didn't read the documentation you so "helpfully" referred to. But boldly claim that my reply isn't helpful. While at the same time you have no idea, if your reply would help.
In other words, you are guessing, while I know for a fact.
The documentation explicitly states that
Quote:
As of Smarty 3.1 the extends: resource may use any available template resource, including string: and eval:.


Code:
<?php

require_once '_library_init.php';

$smarty = new Smarty();

$smarty->setConfigDir(__DIR__);

if(!is_dir(__DIR__ . '/cache'))
  mkdir(__DIR__ . '/cache');
$smarty->setCacheDir(  $_SERVER['DOCUMENT_ROOT'] . '/cache');

if(!is_dir(__DIR__ . '/cache/templates'))
  mkdir(__DIR__ . '/cache/templates');
$smarty->setCompileDir(__DIR__ . '/cache/templates');

if(!is_dir(__DIR__ . '/plugins'))
  mkdir(__DIR__ . '/plugins');
// add before current plugins directory:
$pluginsDirs = $smarty->getPluginsDir();
array_unshift($pluginsDirs, __DIR__ . '/plugins');
$smarty->setPluginsDir($pluginsDirs);

$smarty->force_compile = defined('TEST_FORCE_COMPILE') ? TEST_FORCE_COMPILE : true;

$smarty->registerFilter('pre',
  function($tpl_source, Smarty_Internal_Template $template)
  {
    $stack = stristr($tpl_source, '__HALT' . '_COMPILER');
    $tpl = substr($stack, strpos($stack, '?>') + 2);
    return $tpl ?: $tpl_source;
  }
);

$t = '{block "title"}Serious title{/block}';
$b = '{block "body"}Page compiled with Smarty {$smarty.version}.{/block}';
$ts = urlencode($t);
$bs = base64_encode($b);

$tpl = 'extends:file:' . strtr(__FILE__, '\\', '/') . "|string:urlencode:$ts|string:base64:$bs";
error_log($smarty->fetch($tpl));
$smarty->display($tpl);

__halt_compiler();
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<title>{block 'title'}Silly title{/block}</title>
</head>
<body>{block 'body'}Silly page body.{/block}</body>
</html>

Works like a charm.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<title>Serious title</title>
</head>
<body>Page compiled with Smarty 3.1.29.</body>
</html>

So much for your "help".
Back to top
View user's profile Send private message
Aristophan
Smarty Regular


Joined: 10 Jan 2011
Posts: 96

PostPosted: Wed Jul 27, 2016 1:37 pm    Post subject: Reply with quote

AnrDaemon wrote:
In other words, you are guessing, while I know for a fact.

Absolutely! But knowing (some) facts and writing really helpful, friendly posts for an answer is not always the same! You proved it every day you have been here. This is what I was referring to. This also is what others call a troll.
Thanks for listening. End of answer!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1615

PostPosted: Wed Jul 27, 2016 2:20 pm    Post subject: Reply with quote

Ok, what in my first post wasn't helpful?
Given that OP didn't gave any specifics to his issue. He didn't even stated the version of Smarty he's using.
For all that I know, it could be Smarty2. Seen that multiple times.
Friendly? I didn't call anyone an asshole. That's enough friendlyness for a stranger on the street.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP