Ok,
So quick question that is driving me crazy with FuelPHP
For css, js, and img assets I can do this in a view
<?php echo Asset::css('main.css'); ?>
But if I try to add a folder - for example - "media" to the public/assets directory I can not do this:
<?php echo Asset::media('myvideo.mp4'); ?>
Does anyone know if there is a way to configure that kind of functionality? Has anyone been able to modify the asset class to do that?
Thank you very much for your time and help!
I don't know if there is such fashion that you could create your own static method name (the one you like is media as I haven't tried it yet). But you declare that path instead. Consider this example:
(file is in public/assets/media/file.mp4)
Controller
Asset::add_path('assets/media/', 'media');
View
Video 1
You can easily create such methods by extending the core class.
This can not be solved generically (for example with a magic __call()), because the class would not know which HTML to generate for "media" (or whatever you want to add).
Related
I am new to PHP, trying to understand how to use a simple template system with PHP, I like "Plates", mainly because it claims to be a template system, not a template language, but the documentation is kind of confusing.
As an example where this code "controller" leaves? Is it a another PHP file?
// Create new Plates instance
$templates = new League\Plates\Engine('/path/to/templates');
// Render a template
echo $templates->render('profile', ['name' => 'Jonathan']);
Shouldn't this code "profile.php" call the controller file somewhere?
profile.php
<?php $this->layout('template', ['title' => 'User Profile']) ?>
<h1>User Profile</h1>
<p>Hello, <?=$this->e($name)?></p>
The template file:
template.php
<html>
<head>
<title><?=$this->e($title)?></title>
</head>
<body>
<?=$this->section('content')?>
</body>
</html>
Does anyone have a better code sample using "Plates" in PHP where I can see how this template system works?
Thank you!
The example in the Plates doc is pretty good and simple. The way I see it - you might have an MVC controller or you might not; the code in question may reside in a simple script (lets say index.php for the sake of clarity). Lets imagine the site structure looks like:
/index.php
/templates/ folder
/templates/profile.php
/templates/template.php
The code in index.php becomes:
// Create new Plates instance
$templates = new League\Plates\Engine('/templates');
// Render a template
echo $templates->render('profile', ['name' => 'Jonathan']);
You might have it in an MVC controller, but that is totally different topic.
I wanted to try a pure php templating engine too so I tried out Plates as well. It is simple. I like simple solutions for simple problems. They are faster to implement.
If you are new to php using Plates can be a great way to start to write 'cleaner' code with very little additional investment of your time.
Because you are new to PHP there is a bit of background detail you need to understand first. Then I think it will be clear.
The example here is not so great in that the paths are not that useful. Assume they intend to show that the fully qualified path is required. A better example would have used something like:
$templates = new Engine(__DIR__ . '/templates');
where the templates directory is a sub-directory of the current folder that the presenter or controller may be in.
Since templates are central to presentation, it is my opinion that presenter is a better name for the class that will be using the templates but a lot of people use controller because of the popularity of mvc.
"\League\Plates\Engine" is a namespace, not a path. You can include a "use" statement in your class for every imported class you want to use from that namespace if you want.
On their "installation" page here they show that you have to edit the composer.json file to load plates. If you are unfamiliar with composer all you have to do is install it and run "composer update" in the folder with the composer.json file from the installation page and it will create the vendor folder for you with all the supporting files necessary to run Plates. It will place these files in a vendor folder which is why you need to require vendor/autoload.php.
Plates also assumes that the files in your template directory are php files and that is why they don't require a file extension on the parameter. so 'template' and 'profile' refer directly to the template.php and profile.php files.
The essential idea behind templating is to separate your view and code. Data is obtained by the code and passed into the view. These template files represent the view and the data is passed into the templates via the associative array in the render function in the example.
Because Plates is a php based templating engine, the templates ARE php files. So some php coding will be required to obtain the data from the model (the associative array) and for looping constructs and such. What you want to achieve is to keep the code specific to only what is necessary to render the data in the html and nothing else.
The profile.php page and the template.php page are both "templates" The template.php file is useful if you are going to display a whole page but unnecessary if you just want to output an html element. In the latter case the profile.php file in the example could be produced without the optional page 'layout'.
So what the 'render' function basically does is to load the php file specified by name and insert all the data from the associative array into that file according to its own code and optionally its associated layout template file and return the resulting html.
The class using the templates let's call it Presenter.php should have code something like this in it:
<?php
require 'vendor/autoload.php';
use \League\Plates\Engine;
class Presenter {
private $name;
function __construct($name){
$this->name = $name;
}
function render() {
$templates = new Engine(__DIR__ . '/templates');
echo $templates->render('profile', ['name' => $this->name]);
}
}
and an index.php like this:
<?php
require(__DIR__ . '/Presenter.php');
$presenter = new Presenter('Jonathan');
$presenter->render();
...
where your file structure in your project directory looks something like this:
index.php
Presenter.php
templates/template.php
templates/profile.php
composer.json
composer.lock
vendor/...
That should get anyone interested in trying Plates started.
I'm almost done writing a very simple module for zf2. One thing I'd like my module to do is to inject some css to the layout so that the HTML it generates displays in a nicer way.
Is this possible to do from within a module? If so, how?
EDIT: Thank you all for the prompt responses. However I think I probably didn't explain myself very clearly. When I say "inject some css" I mean taking a string of css and having it actually rendered INSIDE the layout. I didn't mean linking to an external css file or having an asset manager publish my files like the answers so far have suggested.
See Publishing assets from modules in Zend Framework 2 or How to merge Zend Framework 2 module public directories for discussion of the options you have for pushing public assets from a module.
And in addition to pushing your module assets to public, you could put the append into a triggered method like onBootstrap:
public function onBootstrap($e) {
$sm = $e->getApplication()->getServiceManager();
$headLink = $sm->get('viewhelpermanager')->get('headLink');
$headLink->appendStylesheet('/assets/MyModule/css/mystylesheet.css');
}
Try to use something like:
$sm = $this->getEvent()->getApplication()->getServiceManager();
$helper = $sm->get('viewhelpermanager')->get('headLink');
$helper->prependStylesheet('/css/mystylesheet.css');
in Your module controller.
EDIT:
If you want to store css style in module, You can either render it in Your layout file (head section) or, the better way, create another route in module, for example /get/style/[:name]. This route point to another action which returns only plain text/css document. More or less :)
Add a variable to your layout for where you'd like the CSS to be inserted:
Some Link
Then in your Controller, load and assign it however you'd like:
$this->layout()->CSS = "CSS";
$this->layout()->CSS = $this->getRequest()->getPost('CSStoInject');
$this->layout()->CSS = fopen(), curl(), etc.
When using the image_tag() method like this:
<?php echo image_tag('whatever.png') ?>
Symfony outputs an image like this:
<img src="/images/whatever.png"/>
By default it uses images as the base images folder in your web directory. Can you change this default folder name in the Symfony config? Say I wanted to use img instead of images...
The config variable is named sf_web_images_dir_name. You can see that in the source here. It can be set via sfConfig::set or through app.yml.
All told, it seems like the easiest way to do this is adding:
<?php sfConfig::set('sf_web_images_dir_name', 'img'); ?>
to the end of app.yml, simply because that is where config stuff is expected, but there are a variety of different ways to accomplish this.
I'm not sure if there is a way to configure this globally in a YAML settings file, but this should be easy to do in apps/frontend/config/frontendConfiguration.class.php (or the equivalent for your application name), using sfConfig::set:
<?php
class frontendConfiguration extends sfApplicationConfiguration
{
public function configure()
{
sfConfig::set('sf_web_images_dir_name', 'img');
}
}
I've used codeigniter in the past but on my current project I'm making the switch to Kohana. What is the best practice on constants?
In codeigniter there is the actual constants.php, but going through Kohana's source I'm not seeing something similar.
Never used kohana, but after a quick googling I find that you can use the config API to create your own config that will house the constants you need.
This thread suggests that if you are storing database sensitive items, to place them in the database.php config, etc.. making them relative to the type of data they are storing.
I'm familiar with Kohana, but not CI so much, so I'm guessing a bit to what you mean by 'constants.' I believe the closest thing to this is indeed Kohana's config API. So, if you wanted to make templates aware of some site-wide constant like your site name, that's a great thing to use the config API for.
To accomplish this, you'll need to create a config file under your /config folder, probably in the /application directory. What you call it isn't very important, but since it contains site information, let's call it site.php.
To quickly get going, here is what you'll want to have in that file:
<?php defined('SYSPATH') or die('No direct script access.');
return array(
// Your site name!
'name' => 'Oh me, Oh my',
);
Now, you can bring this in to a template by doing something like:
A better way to do this (using dumb templating) would be to assign this as a template variable in your Controller. So, assuming you have some default controller set up, the code would be:
public function action_index() {
$this->template->site_name = Kohana::config('site.name');
}
And then your template would have something like this:
<title><?php echo $site_name; ?></title>
Kohana's config API is interesting because it is hierarchical, meaning you can override and merge new configuration values on top of existing config structures. When you call Kohana::config('site.name'), the engine looks through all the config files named site.php, runs all of those config files and merges the results in to an array. The application-level config files will overwrite modules, which will overwrite system, etc... Then, based on that result array, Kohana will attempt to find the 'name' key and return it.
Assuming you want global constants...the following worked well for me.
application/config/constants.php:
define('MY_COOL_CONSTANT', 'foo');
return array();
index.php:
Kohana::$config->load('constants');
MY_COOL_CONSTANT should then be available globally.
Is the proper way to make a few variables available to all my controllers to add a MY_Controller.php file in my /application/libraries/ folder (shown in the docs here)?
I'm working in Kohana 2.3.4 and wondering if there are any better ways to do it, or is this the only recommended method?
Being new to OOP, can you link me to any examples?
I've heard the right answer is to add the vars to your $config[], trying to get more details.
The proper way is to make a custom config file (application/config/foobar.php), and access the data with Kohana::config('foobar.key').
The code igniter way is completely wrong and inappropriate.
See http://docs.kohanaphp.com/core/kohana#methods_config
How does this feel then:
[bootstrap.php]
Kohana::$config->attach(new Kohana_Config_File('global'));
And then, create a new file under application/config called global.php
In it, put (for example):
return (array ('MyFirstVar' => 'Is One',
'MySecondVar' => 'Is Two'));
Anywhere in your code, access these variables with
Kohana::config ('global.MyFirstVar');
As you can see, 'global.' is used to access these variables; the reason for that is that you attached the global.php config file at the beginning.
Was this what you meant? :-)