Templates stored in database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Allen

    Templates stored in database

    Hi,
    I have a form where a user can enter different feild, for simplicities sake,
    say a 'To:', 'From' and 'Message' feild. The can select a 'template' from
    the databse, each one has different layouts, images etc, and is sotred as
    HTML code in the mySql. My question is how do I insert the vairables into
    the correct places in the template? I realise if it was hard-coded I could
    use something like:

    echo "To:". $to. "<br><br>". $message. "<br><br>From:" . $from;

    etc, although this is simplified alot and the ones int databse contain
    images, tables, css etc. I don't really want to hardcode them using IF
    statements as I want it to be easy to add new 'templates'.


    Thanks
    Ben


  • Rik

    #2
    Re: Templates stored in database

    Ben Allen wrote:[color=blue]
    > Hi,
    > I have a form where a user can enter different feild, for
    > simplicities sake, say a 'To:', 'From' and 'Message' feild. The can
    > select a 'template' from the databse, each one has different layouts,
    > images etc, and is sotred as HTML code in the mySql. My question is
    > how do I insert the vairables into the correct places in the
    > template? I realise if it was hard-coded I could use something like:
    >
    > echo "To:". $to. "<br><br>". $message. "<br><br>From:" . $from;
    >
    > etc, although this is simplified alot and the ones int databse contain
    > images, tables, css etc. I don't really want to hardcode them using IF
    > statements as I want it to be easy to add new 'templates'.[/color]

    If you have a limited amount of variables, you could look into the
    (s)printf() function.
    printf('To:%1$s <br><br>%2$s<br ><br>From:%3$s' , $to, $message, $from);

    Else, you could consider a kind of syntax of your own, like
    '{{{var_name}}} '.
    $to = 'Me';
    $from = 'You';
    $message = 'message in this case';
    $string = 'To:{{{to}}}<br ><br>{{{message }}}<br><br>From :{{{from}}}';
    echo preg_replace('/{{{([a-zA-Z0-9_\-]*)}}}/e','$$1',$strin g);

    I don't really like the e-modifier in this case, maybe others have a better
    solution for this. Don't come knocking with eval(), that's even worse :P

    Grtz,
    --
    Rik Wasmus


    Comment

    Working...