Welcome Guest, Not a member yet? Register   Sign In
Template Parser Class Improvement
#51

[eluser]dmyers[/eluser]
Any Chance we can get a {IGNORE} or something? this is really playing havoc with my jquery onload stuff since Javascript uses { } for a lot of stuff!

$().ready(function(){
$("#selector").addClass("something");
});

In this simple example it blows out the entire onload between the { }!!!

Ah.....
#52

[eluser]GDmac - expocom[/eluser]
Hello all. I've made a mashup of the various versions in this thread
Template Parser Extended is attached to this post.
@dmyers, ignore and ignore_pre tags are added.

the parser_test.php controller has two fixme() tests.
<del>fixme1 shows a problem when a variable with the same name 'title' is stored before an array which also has an element named 'title'</del>.
fixme2 has to do with preg_match (which i know very little about). Stripping tags produces errors on javascript.

<del>Updated: version 0.4.1 attached.</del>
Updated: version 0.5.0 attached.)
- version 0.5.0 supports nested conditionals.

Please test, debug, audit. Hope it is usefull. Probably works in php5 only (can somebody test that?).

[pre] * Library: Extended Parser Class (2008-2010)
* Features: Parse templates, loaded as a view, and/or parse strings;
* Nested simple conditionals {if *}x{else}y{/if}.
* Nested conditionals (introduced in version 0.5.0);
* Array-element access; Successive same array usage;
* {Ignore} tag-pair protects a section from strip_vars but tags inside
* it are parsed. {Ignore_pre} is saved early on, and not parsed at all.
* You can use multiple ignore and ignore_pre tag-pairs.
* Tips: - Use Parse() to load a standard view with the php being processed.
* Parse_string() allows you, for instance, to build an email-message
* with simple template logic, or a partial view.
* - The cleanup unused tags option (strip_vars) is optional and should
* IMO be avoided. Better is to set all variables, be it empty. The parse
* will then automatically replace them with empties. Currently strip_vars
* generates a php error on various curly-brackets like inside javascript.
* For the time being, either protect javascript with ignore, or don't
* strip tags. Another reason to not strip_vars is, it also removes tags
* that might be in your data, like an article discussing parser {tags}.
* Version: 0.5.0
* Changes: 0.5.0 gdmac. nested conditionals. option (default) to convert
* delimiters inside data to hmtl-entities.
* 0.4.1 gdmac. fixme1 fixed, process all tag-pairs before singles.
* changed: remove ignore tags, even when strip_tags==false
* 0.4 gdmac. Mashup of parser versions, ignore tag, extra code and debug
* 0.3 rafsoaken. cleanup, test-suite, parse, array, booleans etc.
* 0.2 isaiahdw. conditionals
* 0.1 adamp1. cleanup unused tags and tag-pairs
* Todo: - To get this going, we need some nice documentation with it
* - set_options and better error-reporting and handling (malformed tags).
* - fix for stripping tags on curly brackets like in javascript
* - option to check for empty (next to isset)
* - conditional combinations {if {is_admin}==1 AND {username}==Tim}
* - general optimization and debugging
*
* Install: Put in /application/libraries/ as parser.php instead as MY_parser.php
* Since most methods of the class changed, it made sense to not extend
* the default parser anymore.
*
* Discuss: http://ellislab.com/forums/viewthread/68878/P45/
*
* usage: See the parser_test controller for some examples

[/pre]
#53

[eluser]GDmac - expocom[/eluser]
Version 0.5.0 attached to previous post, now supports nested conditionals.
(also thanks to adamp1, isaiahdw and rafsoaken for the first versions of the library)
You now can use nested conditionals like these. Some testing is appreciated.

Code:
{posts_array}
    <h2>{title}</h2>
    <p>{body}</p>
    {if {post_views}>=100}
      This post is {if {post_views}>=200} very {/if} popular.
    {/if}
{/posts_array}

delimiters in data &_123;if number&_125&_123;test&_125&_123;/if&_125
are now converted to html-entities by default.

Working with nested conditionals
{if {myarray title}=="ci"}
  this text shown if key 'title' of array 'myarray' has value  'ci'
  {if {boolfalse}}
     im not shown, false is treated as 0
  {else}
    {if "{foo}"=="bar"}foo is bar{/if}
    this text is shown too
{/if}
#54

[eluser]Juan Ignacio Borda[/eluser]
Excelent contribution this was just what i needed I spect this improvement to be in next releases
it's light and effective.
Thanks!
#55

[eluser]andyy[/eluser]
For some reason:
Code:
{ignore}
$(document).ready(function(){
//js here
});
{/ignore}

is not working properly. The ignore tags are not appearing in the final view which leads me to believe they are being handled, but my javascript in between them is getting molested (the curly brackets are being converted).

Any help would be great..
#56

[eluser]GDmac - expocom[/eluser]
Hello Andyy, it's best to first test it out on a seperate view or string with some test-data, that helps troubleshooting.

Also from your question it's almost impossible to see what you're doing or even trying to do. What other settings do you use? do you use strip_vars? Do you have multiple {ignore} tags, if you don't use template-variables in your javascript, you could use {pre_ignore}.
#57

[eluser]andyy[/eluser]
[quote author="GDmac - expocom" date="1288177371"]Hello Andyy, it's best to first test it out on a seperate view or string with some test-data, that helps troubleshooting.

Also from your question it's almost impossible to see what you're doing or even trying to do. What other settings do you use? do you use strip_vars? Do you have multiple {ignore} tags, if you don't use template-variables in your javascript, you could use {pre_ignore}.[/quote]
Instead of outputting the parsed template:
Code:
$this->parser->parse('template_name', $pass);
I was returning it as a string for a jQuery callback and echoing the output:
Code:
echo $this->parser->parse('template_name', $pass, true);
In either case, it wasn't working properly. I tried {ignore_pre} (what your documentation has indicated is right), but I see you've stated {pre_ignore} which I didn't try. I'll have to create a test setup and let you know of the results, but I'm positive I tried all avenues and in the end my curly brackets were still being converted within the parsed templates javascript block.
#58

[eluser]Unknown[/eluser]
I tried using this parser and I get an unable to convert string error on line 415.
Has anyone else run into this.
#59

[eluser]darkhouse[/eluser]
I need to be able to do something like this:

Code:
{if {logged_in:admin}}
<li><a href="admin">Admin Dashboard</a></li>
{/if}

But I get the error: Error. Found delimiters in condition to test : {logged_in:admin

Is this something that's being looked into? I found where the error is being thrown in the code, but simply commenting out that block didn't seem to do anything - it didn't give me an error, but it didn't show the link either. It's also after 3am, and I didn't feel like really scouring the script to see where the issue is occurring.

Thanks.
#60

[eluser]GDmac - expocom[/eluser]
Darkhouse, i don't think "logged_in:admin" is a valid variable reference.
see the example above.
Code:
// check an array value
{if {myarray key}=="value"} ...
//or check a variable value
{if {logged_in}=="admin"} ...
// or check if a variable is set (or boolean)
{if {logged_in}} ...




Theme © iAndrew 2016 - Forum software by © MyBB