Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Trying to upgrade, having trouble with slashes and capture
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
qUJy5Xg0ve7k
Smarty Rookie


Joined: 15 Aug 2018
Posts: 15

PostPosted: Wed Aug 15, 2018 8:54 pm    Post subject: Trying to upgrade, having trouble with slashes and capture Reply with quote

I upgraded version by version and made a bunch of changes to the point where my page finally renders. That's some accomplishment. But versions 3.1.28 through 3.1.32-ish complain about slashes such as:

Code:
{include file='somedir\somefile.tmpl'}


If I switch the slash it's happy. Now this problem goes away in version 3.1.32 (possibly 3.1.31, I didn't test that one) but that version has a NEW problem:

Code:
{capture name=FooBar}I am the foobar{/capture}
{assign txt 'FooBar'}
<script>console.log("{$smarty.capture.FooBar}");</script>
<script>console.log("{$smarty.capture.$txt}");</script>
<script>console.log("{$smarty.capture[$txt]}");</script>
<script>console.log("{$txt}");</script>


This prints out

Code:
I am the foobar


FooBar


In older versions it worked fine. Even though it says $smarty.capture is an array and $smarty.capture['myVar'] works, smarty.capture[$myVar] does NOT work even though they're the same thing when $myVar='myVar'.

What gives? Is there a workaround? It seems like I can't upgrade past 3.1.27 if I want everything to work properly.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Thu Aug 16, 2018 2:20 am    Post subject: Reply with quote

Can you help us by indicating which variation of $smarty.capture gives nothing and which gives something.

I can guess that the first and fourth variant gives something.

The first seems to agree with "Example 7.21. {capture} with the name attribute".

The fourth is from a simple assignment. The string 'FooBar' is assigned to the variable txt. There are some examples in the {assign} documentation where the name of the variable is not in quotes, and some that are.

The third variant seems to be restricted to usage in section loops. See "Example 3.2. Variables".

The second variant seems legit, but we must determine if $smarty.capture allows for a key as a variable, instead of requiring the actual string name of a capture block.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Aug 16, 2018 3:37 am    Post subject: Reply with quote

1. Use forward slashes. Always use forward slashes, when writing paths by hands.
2. What is the exact error message you receive? Can you produce a self-contained test-case?
Back to top
View user's profile Send private message
qUJy5Xg0ve7k
Smarty Rookie


Joined: 15 Aug 2018
Posts: 15

PostPosted: Thu Aug 16, 2018 4:18 am    Post subject: Reply with quote

@bsmither you are correct, the first and fourth lines print, with two blank lines in the middle. I believe the middle two print out in 3.1.27, I know for a fact the second one does because that's how the code is currently. The third line was just an attempt to use it as an array because that's what gettype said it was.

@AnrDaemon The code in my initial post simply prints out blank lines. Nothing is logged in the error log. The problem I'm trying to solve is how to get a captured value by name. It worked in 3.1.27 and older.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Thu Aug 16, 2018 5:54 am    Post subject: Reply with quote

Here is a Github issue:
https://github.com/smarty-php/smarty/issues/366

And another:
https://github.com/smarty-php/smarty/issues/135

Maybe they are relevant, maybe not.

If you find yourself needed to update code, perhaps use the assign attribute:
Example 7.22. {capture} into a template variable
Back to top
View user's profile Send private message
qUJy5Xg0ve7k
Smarty Rookie


Joined: 15 Aug 2018
Posts: 15

PostPosted: Thu Aug 16, 2018 5:55 pm    Post subject: Reply with quote

Alright so here is an entire piece of code:

test.php:

Code:
<?php

require_once 'smarty/Smarty.class.php';
 
$smarty = new Smarty();
$smarty->addTemplateDir('../Smarty');
$smarty->display('./test.tmpl');

?>


test.tmpl:

Code:
<?xml version="1.0" encoding="utf-8"?>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Test Page</title>
</head>

<body>
    Hello
   {capture name=FooBar}I am the foobar{/capture}
   {assign txt 'FooBar'}
   <script>console.log("1: {$smarty.capture.FooBar}");</script>
   <script>console.log("2: {$smarty.capture.$txt}");</script>
   <script>console.log("3: {$smarty.capture[$txt]}");</script>
   <script>console.log("4: {$txt}");</script>
   <script>console.log("**********************************");</script>
   <!-- just in case it's caps lock related -->
   {capture name=baz}I am the baz{/capture}
   {assign txt2 'baz'}
   <script>console.log("5: {$smarty.capture.baz}");</script>
   <script>console.log("6: {$smarty.capture.$txt2}");</script>
   <script>console.log("7: {$smarty.capture[$txt2]}");</script>
   <script>console.log("8: {$txt2}");</script>
</body>
</html>


Output on 3.1.32:
Code:

1: I am the foobar
2:
3:
4: FooBar
**********************************
5: I am the baz
6:
7:
8: baz


Output on 3.1.27:
Code:

1: I am the foobar
2: I am the foobar
3: I am the foobar
4: FooBar
**********************************
5: I am the baz
6: I am the baz
7: I am the baz
8: baz
Back to top
View user's profile Send private message
qUJy5Xg0ve7k
Smarty Rookie


Joined: 15 Aug 2018
Posts: 15

PostPosted: Sun Aug 19, 2018 9:27 pm    Post subject: Reply with quote

Okay so it seems like this is a bug. How do I report a bug?
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sun Aug 19, 2018 11:07 pm    Post subject: Reply with quote

Join the Github:
https://github.com/

Read and Search everything there is that may be related to your observations. I gave links to two such issues earlier:
https://github.com/smarty-php/smarty/issues/366
https://github.com/smarty-php/smarty/issues/135

If you have found nothing, click the green "New Issue" button. Write up your observations.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Aug 20, 2018 9:48 am    Post subject: Reply with quote

It's not a bug, it's a feature request. Which is unlikely to be fulfilled.
You can already assign= capture results to a named variable.
Code:
<?php

require_once 'smarty-config.php';

$smarty->display(__FILE__);

__halt_compiler()
?><?xml version="1.0" encoding="utf-8"?>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Test Page</title>
</head>
{capture name=FooBar assign=FooBar}I am the foobar{/capture}
{capture name=baz assign=baz}I am the baz{/capture}
{$txt='FooBar'}
{$txt2='baz'}
<body>
  Hello!
  <script>console.log("$smarty.capture.FooBar: {$smarty.capture.FooBar}");</script>
  <script>console.log("$txt: {$txt}");</script>
  <script>console.log("$smarty.capture.$txt: {$smarty.capture.$txt|default:"-"}");</script>
  <script>console.log("$smarty.capture[$txt]: {$smarty.capture[$txt]|default:"-"}");</script>
  <script>console.log("$FooBar: {$FooBar|default:"-"}");</script>
  <script>console.log("**********************************");</script>
  <!-- just in case it's caps lock related -->
  <script>console.log("$smarty.capture.baz: {$smarty.capture.baz}");</script>
  <script>console.log("$txt2: {$txt2}");</script>
  <script>console.log("$smarty.capture.$txt2: {$smarty.capture.$txt2|default:"-"}");</script>
  <script>console.log("$smarty.capture[$txt2]: {$smarty.capture[$txt2]|default:"-"}");</script>
  <script>console.log("$baz: {$baz|default:"-"}");</script>
</body>
</html>
Back to top
View user's profile Send private message
qUJy5Xg0ve7k
Smarty Rookie


Joined: 15 Aug 2018
Posts: 15

PostPosted: Mon Aug 20, 2018 2:24 pm    Post subject: Reply with quote

Your example code shows the exact same behavior as mine. $smarty.capture.$txt does not print.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Aug 22, 2018 7:26 am    Post subject: Reply with quote

Of course. Nobody said it would.
Back to top
View user's profile Send private message
qUJy5Xg0ve7k
Smarty Rookie


Joined: 15 Aug 2018
Posts: 15

PostPosted: Wed Aug 22, 2018 2:24 pm    Post subject: Reply with quote

It worked in 3.1.27. It no longer works. Where I come from, that's a bug.

Is there a workaround for looping over a list of values and printing out the captures? It's a very significant use case in my project.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Aug 22, 2018 5:33 pm    Post subject: Reply with quote

It worked by pure accident. Nobody promised that.
Nowhere in documentation it says that you could use variables to specify capture names.
Quite the contrary, it says that if you want capture as a variable, you should use "assign=…".

qUJy5Xg0ve7k wrote:
Is there a workaround for looping over a list of values and printing out the captures? It's a very significant use case in my project.

See the example with creating an array of captures.
Back to top
View user's profile Send private message
qUJy5Xg0ve7k
Smarty Rookie


Joined: 15 Aug 2018
Posts: 15

PostPosted: Wed Aug 22, 2018 7:25 pm    Post subject: Reply with quote

Unfortunately that example does not satisfy my requirement. I have a bunch of things that have been captured (even if I go through and update them all to use assign) and a separate list of strings containing those names/assignments. For example:

Code:
{capture name=foo assign=foo}
This is foo
{/capture}

{capture name=bar assign=bar}
This is bar
{/capture}


Then I have a separate array that contains "foo" and "bar" in any order, or may not contain foo or may not contain bar. I need to display the captures in that order. It was cake before:

Code:
{foreach from=$array key=str}
{$smarty.capture.$str}
{/foreach}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Aug 22, 2018 10:13 pm    Post subject: Reply with quote

qUJy5Xg0ve7k wrote:
Then I have a separate array that contains "foo" and "bar" in any order, or may not contain foo or may not contain bar. I need to display the captures in that order. It was cake before:

Code:
{foreach from=$array key=str}
{$smarty.capture.$str}
{/foreach}

How do you have names of template elements in an array outside the template?
This should have been not possible at all to begin with.

All you've said so far telling me that you are mixing business logic into presentation layer and/or doing too much stuff on a single page.
Neither is right, and you're ought to rewrite your mess sooner or later.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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