I would like to create a dynamic check to see if my CSS file contains the minified version style.min.css instead of style.css.
I have created a development and production mode in production it generates a minified version adding a hash and the development is just regular un minified without the .min
How can I do this the cleaner way? What if I have multiple CSS files, I don't want to copy-paste my code over and over again.
<?php
function nm_enqueue_style() {
wp_enqueue_style('nm-style', get_stylesheet_uri());
$cssFilePath = glob(THEME_DIR . '/build/css/prestigexotics.min.*');
$cssFileURI = THEME_DIR_CSS . '/' . basename($cssFilePath[0]);
if (strpos($cssFileURI, 'min') == false) {
wp_enqueue_style('theme', THEME_DIR_CSS . '/prestigexotics.css', array());
} else {
wp_enqueue_style('theme', $cssFileURI);
}
}
add_action('wp_enqueue_scripts', 'nm_enqueue_style');
You can create a function that would make it for you. It would return an array with what you need.
<?php
function checkCssMin($filePath) {
$cssFilePath = glob(THEME_DIR . $filePath);
$cssFileURI = THEME_DIR_CSS . '/' . basename($cssFilePath[0]);
if (strpos($cssFileURI, 'min') == false) {
return [false, $cssFileURI];
} else {
return [true];
}
}
function nm_enqueue_style() {
wp_enqueue_style('nm-style', get_stylesheet_uri());
wp_enqueue_style('theme', checkCssMin('/build/css/prestigexotics.min.*')[0] ? checkCssMin('/build/css/prestigexotics.min.*')[1] : ('/prestigexotics.css', array()));
}
add_action('wp_enqueue_scripts', 'nm_enqueue_style');
Note: I'm not very used to PHP syntax, but I hope the logic can help you. I'm sure you can simplify it even more.
Solved the issue created a function to dynamically output the JS files.
Related
I am trying to add a script code on all pages of my drupal except a few. Is there any conditionals I can use in html.tpl.php ? or any functions on the template.php to achieve this?
I tried the code below on template.php but no luck :(
Thanks in advance!
function THEME_preprocess_page(&$variables) {
if (isset($variables['node']->type)) {
$variables['theme_hook_suggestions'][] = 'page__' .
$variables['node']->type;
}
//this is what I am trying
if ($variables['nid'] == '77') {
drupal_add_js(drupal_get_path('theme', 'THEME') .'/scripts/path.js');
}
}
You can use hook_page_build(&$page)
function MYMODULE_page_build(&$page){
$nids = array(123, 1234); // your nids
if(($node = menu_get_object('node', 1)) && in_array($node->nid ,$nids) ){
drupal_add_js(drupal_get_path('theme', 'mythemename') .'/scripts/path.js');
}
}
Clear all cache after create this function to see result , also ensure of path script is correct ;)
I'm beginer in smarty My php function is:
public function list_special() {
Session::set('was_in_special', 'true');
if (isset($_GET['showall']) && $_GET['showall'] == 'yes') {
TPL::$s->assign('products', $this->proc->getSpecialProducts('true'));
} else {
TPL::$s->assign('products', $this->proc->getSpecialProducts());
}
TPL::$s->assign('title', 'Specialus katalogas - norėdami papildyti specialiųjų kainų sąraša, susisiekite su savo vadybininku.');
//TPL::$s->assign('products', $this->proc->getSpecialProducts());
TPL::$s->assign("pages_num", $this->proc->get_special_products_pages());
TPL::$s->assign("parameters", Request::$params[1] . "/" . Request::$params[2] . "/");
echo TPL::$s->fetch('catalog.products.html');
}
I want call this function in one page , I try use {products} but this nothing display for me
Maybe you should register the object in before display, then call method in template like this:
{object->method p1='xx' p2='xx'}
look here.
http://www.smarty.net/docsv2/en/advanced.features.tpl
I'm in the process of working on my first code igniter project. I have some javascript that I need to write to initiate an instance of something on document ready. It's a one off, will never be called again. I need to include it in the head, but there may be a case where I need to do this at the end of the body. So, for example, lets say I have this:
<script>
alert('this is a one off alert');
</script>
What is the best practice in doing this stuff? Is it acceptable practice to put this in the controller? Does it need a model writing for it? Or do I need to create individual views for each script to be in MVC?
Thanks.
JS is part of HTML so it should not be in controller or in model, so it should be in view, as you are using framework then you should keep it in a separate file, you can have separate file for each method in your controller or one single JS file each controller.
also you can make a base_controller in your core folder and extend all your controller with it, there you can set all the default JS and CSS. so if you call an index method for a controller it will load with default JS and CSS and if need to add any new just pass it from the controller as John B said
You can have this function in your helper file
/**
* getting js files from public folder
* #param array $js
* #return string
*/
function js_tag($js) {
$out = "";
if (!empty($js)) {
$js = array_reverse($js);
foreach ($js as $j) {
if (strstr($j, "http:") == "" && strstr($j, "https:") == "") {
if ($j == 'tinymac') {
$out.='<script type="text/javascript" src="' . base_url() . 'jscripts/tiny_mce/tiny_mce.js"></script>' . "\n";
} else {
$out.='<script type="text/javascript" src="' . base_url() . 'public/js/' . $j . '"></script>' . "\n";
}
} else {
$out.='<script type="text/javascript" src="' . $j . '"></script>' . "\n";
}
}
}
$out .= '<script type="text/javascript"> var baseurl = "' . base_url() . '"</script>' . "\n";
return $out;
}
/**
* getting css files from public folder
* #author Amir M
* #param array $css
* #return string
*/
function css_tag($css) {
$out = "";
if (!empty($css)) {
$css = array_reverse($css);
foreach ($css as $c) {
if (strstr($c, "http:") == "" && strstr($c, "https:") == "") {
$out.= link_tag(base_url() . "public/css/" . $c) . "\n";
} else {
$out.= link_tag($c) . "\n";
}
}
}
return $out;
}
and you can call it from controller
public function index(){
$data['js'] = js_tag('one.js', 'two.js' , 'jquery.js');
$data['css'] = css_tag('one.css', 'two.css' , 'jquery-ui.css');
$thuis->load->view('index' , $data);
}
Note: the above method will reverse the array so the last value in the array will come first on the page so keep the jQuery last in the array
in the header which is common on all website
<?php echo $js.$css?>
generally the practice I use is this:
All view files load a common header. And the view files are loaded at the end of the controller.
You can either hardcode or pass the links for scripts you want to initiate from the controller. Hardcoding the script tags is self explanatory. If you want to do it a bit more dynamically, you could set up an array of script sources and just loop through them in the header.
so in the controller
$data['scripts'] = array();
$data['scripts'][] = 'http://yoursourcehere';
or if self hosting:
$data['scripts'][] = site_url('assets/js/yourscript.js');
then the view
if(isset($scripts))
{
foreach($scripts as $script)
{
echo '<script type="text/javascript" src="'.$script.'"></script>';
}
}
so basically you can just put all your custom script in a separate file an load it this way. Its practical because it can load all your scripts at once in a common way.
Im trying to buil a mobile version of my website, usign Cakephp and JQueyrMobile.
For the Cakephp part i opted to use the following in app_controller:
if($this->RequestHandler->isMobile() || isset($this->params['url']['mobile'])) {
$this->layout = 'mobile';
$this->isMobile = true;
}
$this->set('is_mobile', $this->isMobile);
if($this->isMobile) {
$this->action = 'mobile/'.$this->action;
}
Thank i create the /mobile/ folder for every controller/view and it works fine.
I just have a problem with the home.ctp ! i created the alternative mobile version in /views/pages/mobile/home.ctp but it loads the /views/pages/home.ctp.
What should i do in order to redirect to the mobile version of home.ctp usign the code above ?
Thanks!
Have you tried using beforeFilter and afterFilter in your AppController?
I use this for the mobile version of one of my Cake applications:
function beforeFilter() {
if ($this->RequestHandler->isMobile()) {
$this->is_mobile = true;
$this->set('is_mobile', true );
$this->autoRender = false;
}
}
And afterFilter:
function afterFilter() {
if (isset($this->is_mobile) && $this->is_mobile) {
$view_file = file_exists( VIEWS . $this->name . DS . 'mobile/' . $this->action . '.ctp' );
$layout_file = file_exists( LAYOUTS . 'mobile/' . $this->layout . '.ctp' );
$this->render($this->action, ($layout_file?'mobile/':'').$this->layout, ($view_file?'mobile/':'').$this->action);
}
}
This code comes from another question on mobile content in Cake, have a look at this answer for more information. Another answer on that same question deals with layouts.
drupal_add_css('override/files/style.css');
This kind of statement can't ensure the css is loaded last,
how can I do the trick?
Use the group attribute :
drupal_add_css(drupal_get_path('theme', 'mytheme') . '/css/styles.css', array('group' => CSS_THEME);
The CSS gets added in the order drupal_add_css() is called, which depends largely on the weight of the module or theme doing the calling, stored in the system table. Drupal.org's instructions on changing module weight also work for themes.
The other factor determining the order of drupal_add_css() is simply the order in which the containing function is called in the code of Drupal. template_preprocess_page(), for example, is always called after template_preprocess_node(), simply because the node goes in the page.
For Drupal 6 use:
drupal_add_css('site.css', 'theme', '', array('weight' => 999))
if you look in the template.php theme file of the ninesixty theme you will find a solution.
The following function are called from the theme_preprocess_page hook:
function ninesixty_css_reorder($css) {
global $theme_info, $base_theme_info;
// Dig into the framework .info data.
$framework = !empty($base_theme_info) ? $base_theme_info[0]->info : $theme_info->info;
// Pull framework styles from the themes .info file and place them above all stylesheets.
if (isset($framework['stylesheets'])) {
foreach ($framework['stylesheets'] as $media => $styles_from_960) {
// Setup framework group.
if (isset($css[$media])) {
$css[$media] = array_merge(array('framework' => array()), $css[$media]);
}
else {
$css[$media]['framework'] = array();
}
foreach ($styles_from_960 as $style_from_960) {
// Force framework styles to come first.
if (strpos($style_from_960, 'framework') !== FALSE) {
$framework_shift = $style_from_960;
$remove_styles = array($style_from_960);
// Handle styles that may be overridden from sub-themes.
foreach ($css[$media]['theme'] as $style_from_var => $preprocess) {
if ($style_from_960 != $style_from_var && basename($style_from_960) == basename($style_from_var)) {
$framework_shift = $style_from_var;
$remove_styles[] = $style_from_var;
break;
}
}
$css[$media]['framework'][$framework_shift] = TRUE;
foreach ($remove_styles as $remove_style) {
unset($css[$media]['theme'][$remove_style]);
}
}
}
}
}
return $css;
}
Use the weight attribute :
drupal_add_css(drupal_get_path('theme', 'mytheme') . '/css/styles.css', array('weight' => 999));
Note : CSS added in your theme's info will always be last, so use the same function as before to change the weight of your stylesheets declared in theme's info file.
This worked for me. Adds the css after the last css on the page
$module_path = drupal_get_path('module', 'my_module');
drupal_add_css($module_path . '/css/my_module.css', array('weight' => 99999, 'group'=>CSS_THEME));