how to get base url in smarty codeigniter - php

i am trying to use smarty with codeigniter, the thing is i worked with codeigniter but never did worked with smarty, but trying learning it.. :)
right now a little issue is that in codeigniter when i need baseurl i use this
<?php echo base_url('styles/bootstrap/css/bootstrap-theme.min.css'); ?>
but how to use it using smarty templates, i mean in smarty .tpl files instead of
i did it like this but it didnt worked for me..
{base_url('styles/bootstrap/css/bootstrap-theme.min.css');}
but got error. i know its not right way but had to try lol..
any ideas?

You made little typo on ";"
{base_url('styles/bootstrap/css/bootstrap-theme.min.css')}
note :
url_helper must be loaded before displaying template

you can use like this I didn't test this
{'styles/bootstrap/css/bootstrap-theme.min.css'|base_url}
or you can write php code inside template
{php}
$this->assign('my_url',base_url('styles/bootstrap/css/bootstrap-theme.min.css'));
{/php}
{$my_url}
see this http://www.smarty.net/docsv2/en/language.function.php.tpl

I have used this function for my controller in form action to redirect to my method it worked .
{base_url('my_controller/my_method')}

Related

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.

captcha in smarty doesn't work

I have this problem with smarty, I tried to add a captcha in smarty but I can't see the way of make it work, basically the error comes when i try to show the captcha image, in a normal php file I could embed this php script:
<?php
$SampleCaptcha = new Captcha("SampleCaptcha");
$SampleCaptcha->UserInputID = "CaptchaCode";
echo $SampleCaptcha->Html();
?>
But for smarty I did this:
$SampleCaptcha = new Captcha("SampleCaptcha");
$SampleCaptcha->UserInputID = "CaptchaCode";
$captcha=$SampleCaptcha->Html();
$sy->assign('captcha', $captcha);
The think is that the variable captcha does print the whole html to show the captcha but when it prints the image attr src it does it in php still.
<img alt="CAPTCHA" src="botdetect.php?get=image&c=samplecaptcha&t=da491a96235a0d9f5fbeb0d7c0accc89" id="SampleCaptcha_CaptchaImage" class="LBD_CaptchaImage">
I tried to use a smarty plugin function but it does the same.
Any expert in smarty may know the answer.
Many thanks.
Try this.
It may help you as this site has good examples across different platforms.

replacement for php file_get_contents in smarty template?

I need to use PHP file_get_contents() in smarty tpl file. I can't use it in PHP and assign it to smarty template. Because the URL is generated dynamically through loop inside smarty template file. So I'm using smarty plugin function to achieve that task. But I want to know whether is there any way I can use it in template file directly instead of parsing it from plugin file.
I've attached the plugin code which I'm using to achieve this function. Please anyone let me know how to use it in smarty tpl file directly.
function smarty_function_getTitle($params)
{
if ($params['id']) {
$content = file_get_contents("http://youtube.com/get_video_info?video_id=".$params['id']);
parse_str($content, $ytarr);
return $ytarr['title'];
}
}
I've used below code to call it in smarty template:
{getTitle id=$videoId}
Suggestions are welcome!
For those of you reading that didn't read the comments above, myself and OP are both aware that this is not how you use a template engine. He seems to have his reasons for wanting to do this directly in the template rather than a plugin or ahead of time in his code. So don't slag me for demonstrating how, please :)
Here is how you can do it in Smarty.
{"http://youtube.com/get_video_info?video_id=`$videoId`"|file_get_contents|parse_str:$result}
{$result.title}
I did the first part all in one call but if you want to be careful you can split it into multiple calls with checks. But I tested this locally and it worked fine.

How do you include PHP files in the view file of a Zend/MVC?

Maybe I dont understand the MVC convention well enough, but I'm trying to include a file to the index.phtml view for the main Index Controller, and it keeps giving me an Application Error. I have no idea what this error is or why its not working. But I'm using a standard include_once(...) in the view.
Is this even allowed?
A view in Zend is still just a php file. If you are getting errors in a view using include_once(), they are probably because the file you want can't be found in your include path. Trying dumping get_include_path() into the view and you will see what directories PHP is searching to find your included file.
As an alternative to include_once, you could use
<? echo $this->render('{module}/{action}.phtml') ?>
to pull in the file.
There are partial views for such purpose
http://framework.zend.com/manual/en/zend.view.helpers.html (ctrl+f Partial Helper)
The view is only the HTML that will be rendered. It's the very last thing that is processed. The controller is called first, then it calls whatever models are needed within. After passing all the data to the view, the view's HTML is rendered.
In short: whatever you include in the view, the controller isn't aware of it. You need to run your PHP includes earlier in the code. If you do it in the controller, it should work OK, I suppose (not tested, so I don't guarantee anything).
You can use Zend View Helpers for this purpose
last time this works fine for me. You can try this:
<?php echo $this->partial('common/left_menu.phtml'); ?>

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