Smarty problem with {php}

Discussion corner for Developers of Serendipity.
Post Reply
corcons
Regular
Posts: 62
Joined: Sun Jun 30, 2019 11:54 pm

Smarty problem with {php}

Post by corcons » Sun Oct 11, 2020 7:50 pm

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

User avatar
onli
Regular
Posts: 2397
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Smarty problem with {php}

Post by onli » Sun Oct 11, 2020 9:45 pm

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?

corcons
Regular
Posts: 62
Joined: Sun Jun 30, 2019 11:54 pm

Re: Smarty problem with {php}

Post by corcons » Mon Oct 12, 2020 1:48 am

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.

corcons
Regular
Posts: 62
Joined: Sun Jun 30, 2019 11:54 pm

Re: Smarty problem with {php}

Post by corcons » Mon Oct 12, 2020 1:57 am

The PHP should be in the index.tpl "/ templates / timeline" which only Smarty recognizes as PHP.

User avatar
onli
Regular
Posts: 2397
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Smarty problem with {php}

Post by onli » Mon Oct 12, 2020 4:10 pm

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.

corcons
Regular
Posts: 62
Joined: Sun Jun 30, 2019 11:54 pm

Re: Smarty problem with {php}

Post by corcons » Mon Oct 12, 2020 4:26 pm

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);

 }

User avatar
onli
Regular
Posts: 2397
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Smarty problem with {php}

Post by onli » Mon Oct 12, 2020 4:33 pm

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.

corcons
Regular
Posts: 62
Joined: Sun Jun 30, 2019 11:54 pm

Re: Smarty problem with {php}

Post by corcons » Mon Oct 12, 2020 5:02 pm

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 !!!

corcons
Regular
Posts: 62
Joined: Sun Jun 30, 2019 11:54 pm

Re: Smarty problem with {php}

Post by corcons » Mon Oct 12, 2020 7:10 pm

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}

Post Reply