I have to add a feature to a quite big app using zend framework.
i have a view. in that view, i have a if and want to include an other .phtml on the same location.
so, at the moment i got something like
if (x = true)
require_once(the other file);
that works, but isnt in the meaning of zend. i've been told i should use view helpers, more specific, the partial. so, how do i include a phtml file with the partial? i dont get it.
Use the render() view helper like this:
<?=$this->render('the other.phtml')?>
All view variables available in the current .phtml script will be available in the other.phtml too.
If you want to render the other view script with specific view variables, use partial instead:
<?=$this->partial('the other.phtml', array(
'var'=>'value',
'var2'=>'value2',
...
))?>
Related
Im new to October CMS
I read the documentation and its states that components can be used inside a layout on the PHP Section (https://octobercms.com/docs/cms/layouts)
i want to create a component that will be used as configuration file for my theme, declaring global variables, that will be used on all pages, but will also be used for all layouts i will create. but i cant find how to do it via code. Like include a file. i want this to used this parameters inside the PHP Section of the layout and the pages.
If components is not the best way, can you ppl sugest me what is the best way
I did before search a lot to find some way that I can share variables to all my layouts and pages but couldn't find anything.
So I tried my own trick and it worked.
In your frontend you must have header.htm partial. so in the code section in it write a onStart() function and set your global variables so you can access them from any layout or page which includes the header partial.
For example in your header.htm code section:
function onStart() {
$this['my_var'] = ['name' => 'Ahmed', 'age' => 17];
}
That way you can access my_var variable wherever you want in all your layouts and pages which header.htm partial is included.
I have a view helper that needs jquery, jqueryui, knockout and few other js files to work. Some pages already references all the js files needed by view helper, some pages don't.
Right now, I am using the url to figure out if a js file should be referenced or not inside the view helper.
Is there a better way to do this?
Thanks.
I'm not 100% sure I understand your question in the right way, but I'm 100% sure you need to manage your javascript dependencies in a better way.
I usually use require.js module loader to manage javascript files included into a template.
In order to use it, you need to download require.js file and place it somewhere in your project. I believe you would like to place it in ./public/js/vendors directory not to keep it alongside with your own code. Also, you need to create config file for it, where you decide what javascript modules are needed.
So, the concept is pretty clear: if you have javascript files, that are related to some part of your project, for example admin panel, you should create a config that will load those files. I usually have one config for admin panel part of my web application, and another one for user side of it.
This config with require.js itself are included into template like this:
<script data-main="./js/config-admin.js" src="./js/vendor/require.js"></script>
You need to keep in mind, that on every template you should have only one inclusion of javascript - the inclusion of require.js and it's config.
The config usually looks like this:
require.config({
baseUrl: '/public/js',
paths: {
'bootstrap': './vendor/bootstrap',
'jquery': './vendor/jquery'
},
shim: {
'bootstrap': ['jquery']
}
});
require(['./app'], function (app) {
console.log(app); //Here is smth that required module 'public/js/app.js' returns
//Do some coding here if you wish, for example to start js application.
});
If you need more examples of usage, you can visit my github profile, there are a couple of repositories where I use it (symfony and zend2 applications).
If you are not managing your assets via other means, the HeadScript view helper is designed to act as a collection of the scripts that should be rendered in the view.
Considering the helper depends on the JS files, placing these in the helper's factory class would be a logical place.
class MyViewHelperFactory
{
public function __invoke(ViewPluginManager $viewHelperManager, $name, $requestedName)
{
$headScriptHelper = $viewHelperManager->get('HeadScript');
$headScriptHelper->appendFile('/js/file.js');
return new MyViewHelper();
}
}
There are of course better ways of doing these things; personally I like Assetic.
Is it possible to display a view of a component without iframe and plugin?
(That is to say, if possible with a few lines of PHP and maybe SQL queries?)
EDIT:
To be more clear: I'd like to do it directly in the PHP-Template!
(Would be fine to do it in an article as well, as I have written a
PHP-function showArticle(mixed $ident))
(I'm using Joomla 3.5)
I'd like to do something like
<jdoc:include type="component" view="example" name="position-x" />
or
<?php
show_component('component-name', 'view-name');
?>
you can use this component http://extensions.joomla.org/extension/components-anywhere
Install the plugin and enable it.
Then you can call the component this way {component url/of/the/component}
{component index.php?component=com_example&form=1}
Try to use non-sef urls in the url but sef url will still work.
There is another way to achieve this by calling the model into your controller file this way
JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_example/models', 'ExampleModel');
What this does is it searches the model class starting with ExampleModel in the folder specified. here you can eneter just a path string or array of the directories as the first parameter. Next you have to call the method inside the views file this way
$exmodel = JModelLegacy::getInstance('Something', 'ExampleModel', array('ignore_request' => true));
So here you create an instance of the class object which can be used to get the items from the model this way
$items = $exmodel->getitem();
$this->assignRef('items', $items);
next you can copy the default.php file in the tmpl folder of that component and place it anywhere you like inside your layout file. Basically instead of copying the entire component you are calling the model and getting the data which you can use in your layouts.
I am newbie to the zend framework.
I want to know how to includes files in zend framework views.
I am using index.phtml to show the data in zend framework.
index.phtml contains some php pages like:
require_once ('/_incl/functions.php');
include('../functions.php');
Now I want to know where I put these files and call to index.phtml page.
What I can do to include these files, means where is the right place to keep all these files, and call from index.phtml page.
I totally understand how it feels to be a newbie.
No, you do not INCLUDE anything. If there is something you want to happen but have no option for it in zend, you make your own library and structure the class names accordingly. For example:
Zend_Form_Element is a class which is Zend / Form / Element.php
What you do is, make a class file and then, compile it.
To get started I would recommend going through this tutorial on tutsplus.
http://net.tutsplus.com/tutorials/php/zend-framework-from-scratch/
So, make a controller , make views, pass values from controller and then echo it in views.
Try to follow the rule-book rather writing codes in traditional method. :)
Hope you learn faster :)
I have simple way to include file in zend view page.
Just create one folder named 'include' inside public folder and store the file which you want to include.
After that write the following line in above the page where you want to include the file.
**include_once "./<include folder name>/<include file name>.php";**
Thanks......p2c
I am trying to insert a latest tweets "widget" on to a Symfony2 project. I have found an ideal script written in PHP that will do the job perfectly.
However, I don't know where the best place to put 3rd party PHP files in a Symfony2 project. I have placed them in the same folder as all my twig files reside, changed the name to read tweets.php.twig, and even located them in the web folder. When I try to include the file in the twig file that needs the Twitter feed it comes up with an error saying that it can't find the file.
Do I have the right idea, or do I have to convert the PHP in to a twig file or write the PHP script in to a controller?
I believe the recommended way would be to create a Symfony2 bundle that encapsulates all the logic for the tweet widget. You would then call your bundle controller and pass the response to your twig template.
If that is too complicated or you want something more quick and dirty - you can create a controller like TweetWidgetController.php and put the code into there as an action like widgetAction. Just make sure you return the tweet widget output in a Symfony response object.
Then from your main controller - you can do something like
$widget = $this->forward('YourBundle:TweetWidget:widget', array('twitterid' => 'yourtwitterid'));
return $this->render('YourBundle:yourtemplate.html.twig',array('widget' => $widget->getContent()));
Now in your twig template you can put it wherever you'd like by referencing it as:
{{ widget }}