Page 1 of 1

Smarty problem with {php}

Posted: Sun Oct 11, 2020 7:50 pm
by corcons
Hi,
I have a problem with Smarty. I think there was no problem using {php} {/ php} up to version 3.1.

Now that this is over I need a little help. I have to adjust this php snippet for the inserted customer menu.

The snippet is as follows:

Code: Select all

require_once 'libs/Mobile_Detect.php';
$detect = new Mobile_Detect;
if ($detect->isMobile()  ) { 
include('myincludes/mainmenuMobiCell.php');
}else{
include('myincludes/mainmenuMobiBlog.php');
}
}
The snippet should be included here. As you can see there is a piece, but I am missing the second part and the query.

Code: Select all

                                   <li>
                                        <a href="https://corcons.com/">Home</a>
                                    </li>
   				<li><a href="#">Services</a>	
{include 'myincludes/mainmenuMobiBlog.php'}	
				</li>
I hope someone can help me and create a smarty for this situation. The website is: www.blog.corcons.com

Thanks in advance

Hermann

Re: Smarty problem with {php}

Posted: Sun Oct 11, 2020 9:45 pm
by onli
Hey. I'm not sure it gets clear what the question is exactly. So you want to embed a php script into a smarty template. What is not working about that, what information is missing?

Re: Smarty problem with {php}

Posted: Mon Oct 12, 2020 1:48 am
by corcons
Yes that is correct. I have to get the above PHP script configured in Smarty, because I use two menus, one for laptop and one for mobile.

Re: Smarty problem with {php}

Posted: Mon Oct 12, 2020 1:57 am
by corcons
The PHP should be in the index.tpl "/ templates / timeline" which only Smarty recognizes as PHP.

Re: Smarty problem with {php}

Posted: Mon Oct 12, 2020 4:10 pm
by onli
Okay. Please have a look at https://docs.s9y.org/docs/faq/index.htm ... -templates. You can put that PHP code into your templates config.inc.php, then call it as a function in your Smarty template.

Re: Smarty problem with {php}

Posted: Mon Oct 12, 2020 4:26 pm
by corcons
I tried myself here. I only get to the point that the word {detectMobile} must be wrong.
Everything else works one {detectMobile} of course no switching is made.

Code: Select all

 <li><a href="#">Services</a>	
{detectMobile}
{if $is_mobile}
{include 'myincludes/mainmenuMobiCell.php'}
{else}
{include 'myincludes/mainmenuMobiBlog.php'}
{/if}									
</li>
This code is in bundled-libs/Smarty/libs/plugins

Code: Select all

function smarty_function_mobile_detect($params, $smarty)
{
include_once('/home/corconsc/blog.corcons.com/libs/Mobile_Detect.php');
$detect = new Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
$smarty->assign('deviceType', $deviceType);

 }

Re: Smarty problem with {php}

Posted: Mon Oct 12, 2020 4:33 pm
by onli
That does not look like it could work.

See, you define your function in the config.inc.php. Then you have to assign it to Smarty, as shown in the FAQ:

Code: Select all

$serendipity['smarty']->register_function('my_custom_function', 'my_custom_function'); 
That's the only new thing Smarty will understand then:

Code: Select all

{my_custom_function stuff="Cool!"}
So, in the code shown: What is detectMobile? What is $is_mobile? Why is there an include? Include won't work with PHP anyway, not without acitvating it specifically. All of this belongs into the config.inc.php, and then you just call the function that contains that code.

Re: Smarty problem with {php}

Posted: Mon Oct 12, 2020 5:02 pm
by corcons
Onli you can do me a favor. Please write the code where it belongs, I'll hand it over. I don't want anything from the smarty. Please help !!!

Re: Smarty problem with {php}

Posted: Mon Oct 12, 2020 7:10 pm
by corcons
I got it. Put this into the bundled-libs/Smarty/libs/plugins. Please contact me, because it is too long to put it here.
At at the .tpl page:
{mobiledetect}
{if $device->isMobile}
{include 'myincludes/mainmenuMobiCell.php'}
{else}
{include 'myincludes/mainmenuMobiBlog.php'}
{/if}