Use PHP inside Handlebars template - php

I'm using NodeJS with Express and Handlebars for rendering my webpage. I am trying to use PHP on my webpage, is this possible with Handlebars?
When I try to use PHP in my Handlebars template file, but it seems like Handlebars can't render PHP? PHP is installed on the webserver and definitely works, but it seems inside my handlebars template, it doesn't.
When I try pasting this into my Handlebars template file: (Inside the body part)
<?php
echo "Test!";
?>`
Nothing is being displayed on the page.
When I try this:
<?php
echo "<p>Test!</p>";
?>
It displays as:
"Test!
"; ?> "
On my webpage.
Am I doing something wrong, or is it not possible to include php in my handlebars file? If so, how can I use php with handlebars?

No, this will not work.
Unless your template file has a php extension or your web server is set to render handlebars files through a php handler. Which I doubt.
You cannot mingle front-end and back-end code.

Related

How to include php code which comes from php variable to php file?

I have php editor.The php code in the editor go to this php variable: $redux_demo['opt-ace-editor-php']. How can I write this variable to php file?
For example, I insert the css code come from css editor like this:
echo '<style type="text/css">'.$redux_demo['opt-ace-editor-css'].'</style>';
Also, <?php tag may can be use top of code in editor or may not.So, the code must work in two scenerio.
So, I wonder can I use <?php tag after another? Or can I use <?php tag in echo function?
PHP code is different from those code running inside the browser. PHP code runs in your server.
If you are sure you want to write code into PHP file. Try use:
file_put_contents("path/to/php/file.php",$content);
And make sure, for every PHP file, a tag should be put at the beginning, like:
file_put_contents("path/to/php/file.php","<?php\n$content");
BTW, be alert and careful when write PHP file dynamically, it may introduce security issue by doing this.

Calling PHP from Smarty template

I am trying to integrate an ad package into my Smarty based site. I have been given the following PHP code that I need to add to my footer file. But due to it being Smarty I need to add the code instead to my php file so that it can be called/included in my Smarty template file. This was the code that I was given and that I need to include:
<?php if(function_exists('ad_footer')) ad_footer(); ?>
But in the php file I am not sure how to achieve this - I have got this example and wanted to know if it looked correct??
if(function_exists('ad_footer')) {
ob_start();
ad_footer();
$smarty->assign('ad_footer', ob_get_clean());
}
Does the above look the right way to do it?
Thanks in advance
I went ahead and created a test page to see if the above worked and it did so will stick with this code.

HTML treat code within brackets as PHP code

I am building my website completely in PHP. I am trying to make it as much flexible as possible.
I have seen there are some softwares made in PHP that are able to get a HTML page, and before showing it, the PHP code recognizes the code inside brackets {PHP Code} as PHP code, runs it and only then shows the final page.
<h1>Hi My Name is {echo $name}</h1>
How can I achieve the same? I know there is Smarty Code. But I do not want to learn Smarty, I just want to know how to check a HTML page with PHP, find every bracket and threat that as PHP before showing the page..?
Can you point me somewhere?
Are you looking for PHP's basic syntax?
If you enable short_open_tags (it usually is enabled by default), this will work:
<h1>Hi My Name is <?=$name?></h1>
otherwise, this will always work:
<h1>Hi My Name is <?php echo $name; ?></h1>
PHP is already a templating language - there often is no need to add another layer of templating on top of it.
I want to keep the template files separated from the php engine
In fact, you don't
Your template files would behave as native PHP files in every way.
So, there is asolutely no [logical] reason to prefer such a strange solution over native PHP.
use the php tags for the echo statement.
<h1>Hi my name is <?php echo $name; ?></h1>
Well, just point apache to index.php which includes phtml templates into itself. Use <?php ?> instead of { }.

Why is my PHP in articles being interpreted literally as a string instead of as PHP?

I am trying to find a way to write pure PHP in my articles with Joomla (currently using 1.5). The closest I got so far was to use a plugin called Sourcerer (see link)
The issue is that the PHP code is actually being rendered as HTML. When I write the below code via the Sourcerer editor:
<?php echo "Hello world"; ?>
This is what is being written to the article:
<span><</span>?php echo "Hello world"; ?<span>></span>
I am afraid that at some point this is going to break my PHP if I come up with more complex code. Also this makes the code hardly readable. Finally the issue with Sourcerer is that you can only insert new code, you can't edit what you previously added.
Does anybody know a way to write pure PHP into articles?
I would like to insist on the fact that it has to be within articles. I already found a way to do it in modules or components, for instance with Jumi.
I have used Sourcerer a couple of times and it does allow one to insert PHP snippets into articles without issues. If your php is being rendered as html, it sounds like the php isn't getting parsed as php - first thing is to make sure that the sourcerer plugin is indeed enabled so it can allow your php scripts to get parsed. (check plugin enabled status here: extensions->plugin manager->sourcerer).
Also, use the sourcerer ("Insert Code") button at the bottom of your WYSIWYG editor when adding your PHP scripts - it keeps the formatting/syntax highlighting and ensures that it is escaped properly with the {source} {/source} tags.
An update, I have the same issue with joomla 3.x, sourcerer, I have tried tinymice, JCKEdidor and JCEEditor. I have now given up with this method.
To insert php code into an article I have used a php include file and all the html is echoed inside this php include file. The php file is then referenced from within the Article Editor.
Those more proficient than me with joomla will no doubt do this correctly using a module or the like.
You can try another plugin called Direct PHP. It's nice and simple to use. Just download and install it and make sure it is enable. After this start writing your php code in joomla article.
Example:
<?php
echo "Hello World!";
?>
it supports all most every PHP functions and you can filter the functions what you don't need it from its settings.
Here is the link for the download.
Download & import plugin sourcerer https://extensions.joomla.org/extension/sourcerer/
{source} [[?php echo "Hello world"; ?]] {/source}
use this. It fine for me.

Can PHP be inserted into a javascript call in my header?

I have a wordpress theme that I like to duplicate. To make things easier on myself, I'm trying to use the bloginfo function to call my jquery file..like this:
<script type="text/javascript" src="<?php echo bloginfo("template_url"); ?>/js/jquery-1.4.2.min.js"></script>
I just can't get it to work. When I check my source file it looks exactly like the code above.
Can this even be done, or should I just forget it? Thanks guys!
Are you sure the above code is actually in a PHP-file and gets parsed by the server? I can't think of a different reason why PHP-code should just be printed and not executed.
See the Referencing Files From a Template chapter in http://codex.wordpress.org/Theme_Development
Whats the name of this file? It should end in .php.
What does the source look like when you view it from the browser?
Are there other places in the file where you use and is that data correct?
Make sure there are no blank lines at the end of the source file. PHP doesn't like blanks lines at the end of the code.
Two problems: No need for the echo in the WP template tag; should be
<?php bloginfo("template_url"); ?>
Template Tags/info « WordPress Codex
And, why are you trying to include jQuery from the template directory, when it resides in wp-includes/js? You should be using Function Reference/wp enqueue script, if your theme doesn't already include jQuery.
You should try to edit your theme's header.php file directly because custom fields of your theme may not be executed by PHP interpreter.
As I've never used PHP, this is only a guess...
PHP is a server side language, and you're tyring to use it on the client side?

Categories