Using {if} within a template causes problems

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
xcone
New Member
New Member
Posts: 5
Joined: Thu Mar 11, 2010 7:33 pm

Using {if} within a template causes problems

Post by xcone »

Hi,

I wish to reload my page content only when pressing buttons on the menu. That way the menu will not reload when navigating. Then I want move the MP3 player I have on my pages to the menu so it'll keep playing when navigating.

Never used AJAX before, but from what I heard of it, it seems likes the solution I need, so I looked into this tutorial for the basics: http://www.tizag.com/ajaxTutorial/

I've got the AJAX part working fine, so now I need to modify the template so it'll only returns a part of the page when using AJAX. I found this topic for this: http://forum.cmsmadesimple.org/index.php?topic=33003.15

According to the topic I can use showtemplate=false. But that'll only return the {content}. I need a bit more however. There are a few smarty tags in the template besides the content tag that also need to execute even when using AJAX. Another possibility is using {if} tags. So when a button on the menu is pressed, I'll append a AJAX=true in the link. Which I check on using: {if $smarty.get.AJAX == "true"}.

The check works fine. I checked that using this code in my template:

Code: Select all

{if $smarty.get.AJAX == "true"}
  AJAX request
{else}
  Not an AJAX request
{/if}
Now when I replace the text with the actual template stuff, an error occurs.
string(115) "Smarty error: [in tpl_body:21 line 132]: syntax error: mismatched tag {/if}. (Smarty_Compiler.class.php, line 2303)"

This is the template as I use it:

Code: Select all

{process_pagedata}
{cms_module module='VisitorStats'}
{register_user_block_tags}

{if $smarty.get.AJAX == "true"}
  <h1>{title}</h1>
  {content}
{else}
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<__html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>{sitename} - {title}</title>
{metadata}
{stylesheet}
</head>
</__body onLoad="MyOnLoad()">
{literal}
<__script__ type="text/javascript">

function loadContent(contentUrl)
{
  var ajaxRequest; 
  try
  {
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
  } 
  catch (e)
  {
    // Internet Explorer Browsers
    try
    {
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
      try
      {
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } 
      catch (e)
      {
        // Something went wrong
        alert("Your browser broke!");
        return false;
      }
    }
  }

  // Create a function that will receive data sent from the server
  ajaxRequest.onreadystatechange = 
  function()
  {
    if(ajaxRequest.readyState == 4)
    {
      document.getElementById('content').innerHTML = ajaxRequest.responseText;
    }
  }

  if (contentUrl.indexOf("?") >= 0)
    ajaxRequest.open("GET", contentUrl + "&AJAX=true", true);
  else
    ajaxRequest.open("GET", contentUrl + "?AJAX=true", true);

  ajaxRequest.send(null);
}

function MyOnLoad()
{
  var menuElements = document.getElementById('menu').getElementsByTagName('a');
  for(var i = 0, maxI = menuElements.length; i < maxI; ++i)
  {
    menuElements[i].onclick = Function("loadContent('" + menuElements[i].href + "');return false;"); 
  }
}

</__script>
{/literal}

<table width="100%" class="canvas bgborder" cellspacing="2">
<tr>
<td class="canvasheaderfill bgmedium"></td>
<td class="canvasheader primarycolumn bgdark">

<div id="header">
  {image src="xconedesign/header-music.png"}
</div>

</td>
<td class="canvasheaderfill bgmedium"></td>
</tr>

<tr>
<td class="canvasmenufill bglight"></td>
<td class="canvasmenu primarycolumn bgmedium">

<div id="menu">
  {menu template='cssmenu.tpl'}
</div>

</td>
<td class="canvasmenufill bglight"></td>
</tr>

<tr>
<td class="canvascontentfill bgbright"></td>
<td class="canvascontent bglight primarycolumn">

<div id="content">
    <h1>{title}</h1>
    {content}
</div>

</td>
<td class="canvascontentfill bgbright"></td>
</tr>

<tr>
<td class="canvasfooterfill bglight"></td>
<td class="canvasfooter bgmedium primarycolumn">

<div id="footer">
  {custom_copyright}
</div>

</td>
<td class="canvasfooterfill bglight"></td>
</tr>
</table>

<div class="canvasbottomfillwrapper">
<table width="100%" class="canvas bgborder" cellspacing="2">
<tr>
<td class="canvasbottomfill bgmedium"></td>
<td class="canvasbottom bgdark primarycolumn">
<div id="bottom"></div>
</td>
<td class="canvasbottomfill bgmedium"></td>
</tr>

</table>
</div>

<__body>
</__html>
{/if}

Somehow the {/if} tags isn't recognized properly. Though I have no clue why? The template works fine without this modification. I've also noticed when I move the {/if} directly below {else}, it works fine. If the {/if} is placed directly after "{sitename} - {title}" it'll only partially load the page, which contains the error:
string(154) "Smarty error: [in module_db_tpl:News;summaryXC:Basic line 27]: [plugin] could not load plugin file 'block.groupbox.php' (core.load_plugins.php, line 124)".

Groupbox is a smarty block tag which I created myself. I admit, it's not created the way it should be, but it has worked fine all this time already. Only now using this {/if} tag it shows up.


If {/if} is placed directly behind " the error mentioned first occurs again, but then on another linenumber:
string(113) "Smarty error: [in tpl_body:21 line 3]: syntax error: mismatched tag {/if}. (Smarty_Compiler.class.php, line 2303)"

The big question: Why can't I put the {/if} tag where I want it? And more importantly, How can I fix it.

Thx in advance. Looking forward for some input!
Jeff
Power Poster
Power Poster
Posts: 961
Joined: Mon Jan 21, 2008 5:51 pm

Re: Using {if} within a template causes problems

Post by Jeff »

I haven't read any documentation to verify this, but I have had similar problem and figured there might be a limit to how many characters can be in between the {if}{else}{/if} calls.
xcone
New Member
New Member
Posts: 5
Joined: Thu Mar 11, 2010 7:33 pm

Re: Using {if} within a template causes problems

Post by xcone »

Thank you for your reply.

If there's indeed a limit to the amount of characters between an {else} and an {/if} tag, then how do I achieve something similar?

I imagine I could create an AJAX template which simply returns the content if in AJAX mode, or references another template if not in AJAX mode. Though I don't know if such a thing is possible?

Pseudo:
{if $smarty.get.AJAX == "true"}
  AJAX only stuff
{else}
  {loadtemplate src="otherTemplateName"}
{/if}
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: Using {if} within a template causes problems

Post by jmcgin51 »

no limitation to # of characters that I'm aware of, at least not in a page, and I can't think why a template would be different.

just for fun, I made a test page, like

{if 1==1}
print $x
{else}
print $y
{/if}

Where $y consisted of 11796 characters, and $x consisted of 9348 characters.  Both numbers far exceed the number of characters shown in xcone's post (2962 chars).  The page rendered fine with {if 1 == 1} and {if 1 != 1}.

hmmmm
Jeff
Power Poster
Power Poster
Posts: 961
Joined: Mon Jan 21, 2008 5:51 pm

Re: Using {if} within a template causes problems

Post by Jeff »

I am not saying this will work, but try separating some code into GCB.
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: Using {if} within a template causes problems

Post by jmcgin51 »

I don't think this is your problem, but shouldn't
  {image src="xconedesign/header-music.png"}
be
 
or, if you really want Smarty
  {html_image file="xconedesign/header-music.png"}
??
xcone
New Member
New Member
Posts: 5
Joined: Thu Mar 11, 2010 7:33 pm

Re: Using {if} within a template causes problems

Post by xcone »

Thank you both for your replies!

@ajprog GCB's look like fun :) Didn't even know they existed until you mentioned them. I'll have a look.

@jmcgin51 If I look on the admin page Extensions ->Tags, there is no html_image-tag, but there is an image-tag. The image-tag automaticly takes care of width, height, alt, title, etc. so I can ignore those attributes until I need them :)
As for the # of characters. It looks like something else is screwing things up then. I could try and remove as much code as possible and figure out what part of it actually screws ups.

Thx again! I have something to continue on now. I'll let you know of the result once I tried these steps.
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: Using {if} within a template causes problems

Post by jmcgin51 »

xcone wrote: @jmcgin51 If I look on the admin page Extensions ->Tags, there is no html_image-tag, but there is an image-tag.
Ok, no problem.  Hadn't checked that.  I just checked the Smarty manual and it listed the html_image tag, so that's where my suggestion came from.  If "image" is a valid tag, that's fine.
Izal

Re: Using {if} within a template causes problems

Post by Izal »

Have you tried {capture} tags may be the answer, you buffer the data in to a variable then output it when needed?
xcone
New Member
New Member
Posts: 5
Joined: Thu Mar 11, 2010 7:33 pm

Re: Using {if} within a template causes problems

Post by xcone »

I went on a little search and destroy mission with this. The result is .. unexpected. The code simply won't understand html tags.

This works fine:

Code: Select all

{if $smarty.get.AJAX == "true"}
  <h1>{title}</h1>
  {content}
{else}

<__html>
</__body>
MyTekst
<__body>
</__html>

{/if}
But this doesn't:

Code: Select all

{if $smarty.get.AJAX == "true"}
  <h1>{title}</h1>
  {content}
{else}

<__html>
<head>
</head>
</__body>
MyTekst
<__body>
</__html>

{/if}
I've tried smarty comments ({**}) but it still fails, and I've tried {literal}{/literal}, which also fails.

I've also tried to put the entire template in my earlier post without the head-tags, which works fine. So, what's the problem with head-tags? Any thoughts?
xcone
New Member
New Member
Posts: 5
Joined: Thu Mar 11, 2010 7:33 pm

Re: Using {if} within a template causes problems

Post by xcone »

I've just tried {capture} as suggested by Izal. {capture} doesn't seem to solve the problem.

Code: Select all

{capture name='allcontent'}
<__html>
<head>
</head>
</__body>
MyTekst
<__body>
</__html>
{/capture}

{if $smarty.get.AJAX == "true"}
  <h1>{title}</h1>
  {content}
{else}
  {$smarty.capture.allcontent}
{/if}
This leaves a blank page without an error. If I remove the head-tags the page shows "MyTekst". So again the head-tags seem to be the problem.

Finally I've also tried the GCB. I've created a GCB containing the head-tags. All the other template stuff still resides within the template itself. No errors, and a good result. Finally :)

But I can't help wondering ... what's with the tags. It shouldn't be so special, should it?
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: Using {if} within a template causes problems

Post by jmcgin51 »

I dunno why your system is working this way.  I tried the following
{if 1 == 1}




you should see this text


{else}




you should not see this text


{/if}
and
{if 1 != 1}




you should not see this text


{else}


you should see this text


{/if}
and they both worked fine

So I don't think it's the tags causing the issues.
cyclerider
New Member
New Member
Posts: 4
Joined: Tue Apr 20, 2010 1:36 pm

Re: Using {if} within a template causes problems

Post by cyclerider »

Maybe the problem is in the script? Look php tutorials
Post Reply

Return to “CMSMS Core”