It seems F3 framework doesn't handle php function calls within a page? I have a php navigation bar, which is uniform site-wide. I call up my layout page in my controller class thus: Template::serve('layout.php'). In the layout page, I include the navigation bar thus: <F3:include href="navbar.php" />. Within the navbar (navigation) file, I call a utility function siteUrl which gets the absolute url to a resource e.g. css or .js file. This function is defined in an include file which I include as follows: require_once "lib/globals.php. Within the navbar.php, I use the siteUrl as follows for example:
<img id="logo" alt="logo" src="<?php echo siteUrl('small-logo.png') ?>" />
This doesn't seem to work. When I view the generated source of the page, the src section of the img tag is an empty string: "". However, when I call the navigation bar from other pages that are not using the F3 framework (i.e. pages that are not being routed by F3::route. Not all pages of the website are routed using F3), it works fine.
What could be the problem? How could I call a php function from within a php page that is being rendered using Template::serve? It seems the entire content between the <?php ?> tag is not being executed when the page is being served by F3. Echo statements are not being displayed. Thanks for responses.
Template::serve() does not allow PHP. It is a templating engine. There are things you can do. You can define a function using F3::set('sum',function($a,$b){return 1+2;}); and then reference that function in the template with {{#sum(1,2)}}. I would re-read the templating documentation on the fatfree site: http://bcosca.github.com/fatfree/#views-templates
Again, the reason PHP is not working is because you are using Template::serve() and are therefore using the templating features of Fatfree. If you want to use PHP, I believe you can use F3::render() instead and it will render the page, allowing PHP, but you will lose all the templating functionality.
you can use raw php within the template tokens wrapped by curly brakets like this:
<img id="logo" alt="logo" src="{{ siteUrl('small-logo.png') }}" />
it will echo it automatically.
but using F3::set('image.smallLogo',siteUrl('small-logo.png')) to define the image paths and grap them with a simple {{#image.smallLogo}} feels much better.
page moved:
Fat-Free Framework 3 Template Directives
Related
I was using phtml files in zend framework. Now I am using .tpl files.
I found how to use html script and all. But when I want to use php code. Then I'm using:
<?php
echo "test";
echo $this->content;
?>
The problem with this is it is in layout.tpl file. Main content is in index.tpl of other module.
Rather than fetching the content of index file It echoing just 'test'.How to make it works?
Edited: I also tried {$this->content}.
If you are using the Smarty Templating Engine and the SmartyModule, then you will have to use Smarty syntax in your view scripts, since the Zend\View\Renderer\PhpRenderer will be overridden by the Smarty Renderer (and the Smarty Templating Engine). Also, if you wish to use layouts with Smarty, please see Smarty's Template Inheritance mechanism. Here is an example:
layout.tpl
<html>
<head>
<title>{block name=title}Default Page Title{/block}</title>
</head>
<body>
{block name=body}{/block}
</body>
</html>
mypage.tpl
{extends file="layout.tpl"}
{block name=title}My Page Title{/block}
{block name=body}My HTML Page Body goes here{/block}
Otherwise, if you are using the PhpRenderer, it will not "recognize" any templating language, even if you change the view script's file extension to .tpl, since it will simply include the content of the view scripts (see lines 502-503 of the renderer's source code). Therefore, as with any include, the PHP code will be executed immediately and stored in the renderer's $__content property. This is probably the reason why your echo command is immediately executed.
So, basically, you will have to choose your renderer (PhpRenderer or Smarty Renderer through the SmartyModule) and then abide by its inner workings (PHP/HTML or Smarty syntax (ex. variables), respectively).
I have a PHP that returns some HTML code, for example, if I execute this in the browser,
http://www.myserver.com/myscript.php?size=300
this will return some html code like this,
'<div><img src="..." /></div>'
I am wondering if it's possible I can call this php script in my html code directly? For example, if I want to use the output from the PHP script in my Wordpress sidebar widget?
You can use file_get_contents() like this:
<div class="sidebar">
<?php
$html = file_get_contents("http://www.myserver.com/myscript.php?size=300");
echo $html;
?>
</div>
If your Current page's extension is not php, you cannot call php in it directly.
You have to use some javascript/jquery/ajax to run the php file externally and load the data into a class or id using one of those javascript based code.
If your current page is already a php file than,you can use include(); or require(); functions and it does the job.
above answers are correct, but that might be not concerned for wordpress, that are of core php functionality.
i have a different way for sidebar use, that's provide by wordpress already.
it is an inbuilt functionality, please take a look towards my answer.
Show active sidebar in Wordpress
How to make wordpress sidebar
you can found ::
try this one, it is different way of using sidebar
[1] put your all "left_bar"(sidebar) code in a new file named "sidebar-left_bar"
[2] save it with header.php, function.php and all files (saving location)
[3] now just use <?php get_sidebar( 'left_bar' ); ?> where you want to use
Thanks
I have built a customized php framework.
I have a view, controllers and model.
In the view, there is variable which i can set.
$js = array('custom/testimonial.js','jquery.js');
At the footer view i have the following code.
-------------------------------------------------*/
if(isset($js)){
foreach ($js as $jsname) { ?>
<script src="<?php echo MAIN_URL; ?>/resources/js/<?php echo $jsname; ?>"></script>
<?php
}
}
Basically, the code in footer will load all the necessary scripts required for that page. In my other pages, i have other(different) javascripts files to include. This mean thats each page will have different javascript.
I would like to minify the js for each of the page and wonder which is the best way. I tried using Assetic (php asset management) with no luck.
Another question is should i minify javascript on the fly? Means when i load my page called testimonial.php it will check what are the javascript required for the page and minify them before displaying. Will that post performance issues even if i cache it.
I am looking for some methods that has high maintainability as i do not have to minifiy all javascript manually, have 40 to 50 plus pages. (each page uses different javascript files, plugins, lib).
Can Assetic do the job for me? Currently i have problems generating a static file for the javascript.
Appreciated any help.
Yahoo (YUI) used to have a compressor/ minifier, but they're now moving to UglifyJS. See:
https://github.com/yui/yuglify
https://github.com/yui/yuicompressor/
There are also several online minification services, based on the YUI compressor.
http://refresh-sf.com/yui/
If I have 10 images, 2 javascript files, and 4 css files that need to be included inside a Ci view... How is the best way to call all the files? I've tried calling all the external files using $this->load->view('image1.png') and $this->load->view('style.css'). But it doesn't seem to work properly. Any ideas on how to better approach this problem?
You're not using the view method correctly there.
You typically assign one view and pass things like js and css to the template. This can vary depending on how you use the framework.
$this->load->view('path-to-view'); will look for a view in the view folder and not an arbitrary file.
You might look into this:
http://codeigniter.com/user_guide/helpers/html_helper.html#img
for loading images, though I personally think it's pointless to call a framework's method for a basic html element like an image.
There are cases when you would use multiple views, like views to be returned as strings - loops and such may need these - but that doesn't look like the case in your question.
Just in case though here's the view docs:
http://codeigniter.com/user_guide/general/views.html
To elaborate further, the general idea is to use the CI controller to handle the data for your page, pass the necessary template data to the template (like your js and css specific to this page) then assign the necessary data to the view and pass that view to the template. You may be wondering what I mean by template too, since out of box CI loads views progressively if you just call them sequentially.
in your controller you may pass the view an array of header info:
$data['css'] = array('some-path.css','another-path.css');
$this->load->view('your-view', $data);
so in your view that handles the header you might call something like this:
<head>
<?php foreach($css AS $c): ?>
<link rel="stylesheet" type="text/css" href="<?php echo $c; ?>">
<?php endforeach; ?>
</head>
Here's a fair link to CI templates:
How to Deal With Codeigniter Templates?
It's a versatile framework with many options for using it however you are most comfortable.
In Kohana 3 bootstrap.php one can define base_url:
Kohana::init(array(
'base_url' => '/foo/',
));
This usually means also moving the /js/, /css/ and other media to that base dir like /foo/js/, /foo/css/. My question is not to discuss good or bad of such.
Is there a built-in way in Kohana to access the base_url from a template (just like in Django you can use {{ MEDIA_URL }}css/)?
You can output the base url as using URL::base:
<?php echo URL::base(); ?>
If you're outputting a url relative to that you probably want URL::site:
<?php echo URL::site('css/'); ?>
Kohana 3 template controllers use the View class to render templates.
Views are normal php files and have no special syntax, so just use the normal <?php ... ?> tags as above.
The View class allows you to declare variables for use in that view, before you render it.
One good way is that in your layout view, in the head of the HTML you put near the <title> tag:
<base href="<?php echo URL::base(TRUE) ?>">
and then, you load your assets this way:
<img src="assets/images/img.jpg" alt="">
The HTML <base> tag is a way of defining a base URL for all the assets in the page. This way you load your image located at /foo/assets/images/img.jpg without making a URL::base() call in every tag. I hope it helps.