Welcome Guest, Not a member yet? Register   Sign In
Template Parser Extended -Updated!
#1

[eluser]Mortred[/eluser]
Updated!

Hello all!

I just noticed that there are no string output options in the Template Parser Class.

So I tinkered the class and found a solution.

In anyway I want to capitalize, lowercase, format numbers, etc. I just add an extra option in the view file of any variable you want to output.


Normally, in the Parser Class, we would do this in the view file:
Code:
<ul>
{blog_list}
    <li>{title} - {content}...</li>
{/blog_list}
</ul>


But now, I can do this:
Code:
<ul>
{blog_list}
    <li>{title:allcaps} - {content:climit:100}...</li>
{/blog_list}
</ul>

That way, I can capitalize the title and limit the content to 100 characters without modifying it in the controller.

There are several string output modifiers you can choose from, most of these came from CI's own helpers. You can also add your own custom modifier, just add a case option in switch menu.

So here is it, just copy the entire code and save as 'MY_Parser.php' in 'system\application\libraries' and edit your autoload.php add the necessary helpsers


system\application\config\autoload.php
Code:
$autoload['helper'] = array('url', 'string', 'text', 'security');

system\application\libraries\MY_Parser.php

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Parser extends CI_Parser {

    function My_Parser()
    {
        
    }
    
    function _parse_single($key, $val, $string)
    {
        $newval = $val;
        $find = "/".$this->l_delim."".$key.".*".$this->r_delim."/U";
        
        preg_match($find, $string, $matches);
        if(!empty($matches))
        {
            $temp = trim($matches[0], "{}");
            $res = explode(":", $temp);
            if(count($res) > 1)
            {
                switch($res[1])
                {
                    case "allcaps" :
                        $newval = strtoupper($val);
                    break;
                    case "money" :
                        $newval = number_format((int)$val, 2, ".", ",");
                    break;
                    case "caps" :
                        $newval = ucwords(strtolower($val));
                    break;
                    case "nocaps" :
                        $newval = strtolower($val);
                    break;
                    case "ucfirst" :
                        $newval = ucfirst($val);
                    break;
                    case "bool1" :
                        $newval = ($val==1) ? "True" : "False";
                    break;
                    case "bool2" :
                        $newval = ($val==1) ? "Yes" : "No";
                    break;
                    case "bool3" :
                        $newval = ($val==1) ? "Active" : "Inactive";
                    break;
                    case "climit" :
                        $int = (count($res)<3) ? 128 : $res[2];
                        $newval = character_limiter($val, $int);
                    break;
                    case "htmlchars" :
                        $newval = quotes_to_entities($val);
                    break;
                    case "wlimit" :
                        $int = (count($res)<3) ? 25 : $res[2];
                        $newval = word_limiter($val, $int);
                    break;
                    case "wrap" :
                        $int = (count($res)<3) ? 76 : $res[2];
                        $newval = word_wrap($val, $int);
                    break;
                    case "hilite" :
                        $str = (count($res)<3) ? "" : $res[2];
                        $color =  (count($res)<4) ? "#990000" : $res[3];
                        $newval = highlight_phrase($val, $str, "<span style=\"color:{$color}\">", "</span>");
                    break;
                    case "safe_mailto" :
                        $alt_text = (count($res)<3) ? "" : $res[2];
                        $newval = safe_mailto($val, $alt_text);
                    break;
                    case "url_title" :
                        $sep = (count($res)<3) ? "dash" : $res[2];
                        $newval = url_title($val, $sep);
                    break;
                    case "remove_img" :
                        $newval = strip_image_tags($val);
                    break;
                    case "hash" :
                        $hash = (count($res)<3) ? "md5" : $res[2];
                        $newval = dohash($val, $hash);
                    break;
                    case "stripslashes" :
                        $newval = stripslashes($val);
                    break;
                    case "strip_tags" :
                        $allowed = (count($res)<3) ? "" : $res[2];
                        $newval = strip_tags($val, $allowed);
                    break;
                    /** other output string format options here **/
                }
                return str_replace($matches[0], $newval, $string);
            }
        }
        return parent::_parse_single($key, $val, $string);
    }
}


Some examples in your view:
Code:
<table>
    <tr>
        <td>Item: {item_name:ucwords)</td>
        <td>Price: {price:money}</td>
        <td>Description: {description:wlimit:20}</td>
        <td>Email: {email:safe_mailto:Email Contact}</td>
        <td>Avaliable: {status:bool2}</td>
    </tr>
</table>


You can add your own format options there..

I just thought I'd share.. Thanks..

P.S.
It will still work if you don't put any output options.
#2

[eluser]Mortred[/eluser]
Updated..

For a more effective preg_match() and added several output modifiers..

Smile
#3

[eluser]Mortred[/eluser]
Updated:

Improved code structure..
#4

[eluser]easylancer[/eluser]
when using a modifier with the variable once in the page it works fine but if you try to use the same variable twice but with different modifier it doesn't render the second one. eg.
Code:
{title:allcaps}
{title:caps}




Theme © iAndrew 2016 - Forum software by © MyBB