-3

I was wondering if someone can help me out with this. For my system I made i'm using .tpl files for viewer files. The problem underlays in using if else statements.

Like this would be an example of an tpl file:

<div class="menu">
    <li>Home</li>

    <!-- Checking if has admin rights -->
    [IF='isadmin']
        <li>Admin</li>
    [ENDIF]
    <!-- End of checking -->
</div>

But how can I implent this in PHP?

I already searched in stackoverflow, but only found answers as you just need to use PHP for this. This isn't the right way for me to use PHP. The system is already too complicated to change everything to PHP and using PHP as viewer doesn't pass the requirements.

Thanks in advance!

Sentmen
  • 30
  • 5
  • 2
    What's the name of this template engine? – u_mulder Mar 08 '18 at 09:13
  • There is no template engine. This has to be done in vanilla PHP – Sentmen Mar 08 '18 at 09:14
  • 2
    What's the problem then? You don't know how to write `if` in php? – u_mulder Mar 08 '18 at 09:15
  • @u_mulder the problem is he wants his homework to be done for him. – Kevin Kopf Mar 08 '18 at 09:15
  • I recommend "Savant"/"Savant2" template engine. It uses PHP as a templating language, so you don't need to learn the language of a new template engine. "Mustache" is also interesting because there are so many implementations for different languages for it. – Adder Mar 08 '18 at 09:20
  • No. Take a look at the example file. I don't go to school, so I don't have homework like that. @Adder: I don't want to use an template engine but only an simple way to get this working. – Sentmen Mar 08 '18 at 09:31
  • 1
    It looked a bit like you wanted to write your own template engine. – Adder Mar 08 '18 at 09:34
  • Yes @Adder, you are right! But the thing is that it has to be such more simple than an template engine. Just if and else statements are needed. Nothing more. – Sentmen Mar 08 '18 at 09:41

1 Answers1

1

Your file extension must be not ".html". You need to change it to ".php" Then you can use PHP in HTML code like :

<div class="menu">
    <li>Home</li>

    <!-- Checking if has admin rights -->
    <?php if ($isAdmin) { ?> // $isAdmin is a boolean
        <li>Admin</li>
    <?php } ?>
    <!-- End of checking -->
</div>

You can affect a boolean into $isAdmin as you want like $isAdmin = true;. If this var is true, <li>Admin</li> will display.

PS : This notation if ($isAdmin) it's the same thing as if ($isAdmin == true).

Thrilly
  • 27
  • 1
  • This isn't the way I want. If you read my question, you saw that it's impossible to me to change everything to .php and use PHP code inside the template file. – Sentmen Mar 08 '18 at 09:34
  • You could enable php for all .html files in a certain directory: https://stackoverflow.com/questions/4687208/using-htaccess-to-make-all-html-pages-to-run-as-php-files – Adder Mar 08 '18 at 09:46