0

I made one language file and I included in my html code. Like for writing Student Name, I DEFINE in my language file as STN_NAM, and I am writing in html code as

<?php echo STN_NAM; ?>

codes in my language file is

<?php
$nm = array(
    'PET_NAM'   => "Student Pet Name",
    'STN_NAM'   => "Student Full Name",
    'STN_FAT'   => "Father Name",
);

foreach($nm as $key => $val)
    define($key, $val);
?>

Always I have to write this common code as <?php echo xxxx; ?>. Is there any way to just by writing {STN_NAM} it should print full student name.

The result will look like this

<td>{STN_NAM}</td>

This will help me and many other alot if I can get this....

5
  • 6
    On Sackoverflow it makes not much sense to ask if there is a way. In programming most often there is a way, so the answer is yes. What you ask about is commonly called a template engine. Please research first, otherwise your question is too broad. Commented Jul 1, 2013 at 7:48
  • Ok its fine, I will modify my question to suggest the code.. thanks Commented Jul 1, 2013 at 7:49
  • It's possible, but it complicates matters a lot - you'd have to have a real template engine that parses the code for occurrences of {xxxxx} and replaces it with the value in question. Ask yourself very carefully whether it's worth the extra complexity. Commented Jul 1, 2013 at 7:49
  • Well, what would be interesting to know for example which of the common template engines didn't do it so far for you and why? Related: Is include()/require() with “side effects” a bad practice? and Creating a simple but flexible templating engine Commented Jul 1, 2013 at 7:49
  • 1
    possible duplicate of php variable in html no other way then: <?php echo $var; ?> Commented Jul 1, 2013 at 8:00

3 Answers 3

3

How about (in a local scope):

<?php extract($nm); ?>

...

<td><?= $STN_NAM ?></td>

It's still a little repetitive but better. You could also implement a templating system as others have suggested.

Sign up to request clarification or add additional context in comments.

1 Comment

@hakre - Thanks, was trying to find the syntax as I wasn't 100% sure.
2

I really think you should adopt Smarty template engine as a standard php lib for your projects.

http://www.smarty.net/

Name: {STN_NAM}<br>

From: php variable in html no other way then: <?php echo $var; ?>

Comments

0

Not in raw PHP. You can of course use a templating system (written in PHP) to enable this kind of syntax. Twig, Mustache and others use that or a similar syntax.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.