How Stop Loading CSS file Just in index (yii2) - css

i want load site.css in all pages except index
how can ignore this css load in just actionIndex ?
i put site.css in Assest so load in all actions

You could use a specific layout for index (or for all the view where you don't need appAsset )
and in this layout load a different asset that don't involve the css you don't need
eg : in you you could create in you asset directory a MyCLeanAppAsset.pho with you specific asset
view/layout a new layout for manage this situation eg: clean_main.php
<?php
use backend\assets\AppAsset;
use common\widgets\Alert;
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
/* #var $this \yii\web\View */
/* #var $content string */
MyCleanAppAsset::register($this); // a invocation for a differente Asset
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>">
......
......
and in your controllerAction
public function actionIndex()
{
$this->layout('clean_main');
return $this->render('index');
}

Related

disable a css only for one layout yii2

I am trying to integrate a custom theme on YII2 basic app.
I have two layouts, the main layout and other is login layout.
Now I don't need a css file say xyz.css on login layout but it gets loaded there and my design is getting messed up. Any proper way of disabling it on that one layout?
I am registering my css files from AppAsset.php file.
the css section look like
public $css = [
'themes/mytheme/assets/css/xyz.css',
'themes/mytheme/assets/css/main.css'
];
Step - 1: Create LoginAsset.php in assets Folder.
LoginAsset.php
In this file, Keep those .css & .js which is required for login.
<?php
namespace app\assets;
use yii\web\AssetBundle;
class LoginAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/include-your-css-if-any.css'
];
public $js = [
'js/include-your-js-if-any.js',
];
}
Step - 2: Since, you told that you are having one more layout i.e. login layout. So, Use LoginAsset.php in your login layout, Like:
login.php (One out of two layouts i.e. main/login.php)
<?php
use yii\helpers\Html;
use app\assets\LoginAsset;
LoginAsset::register($this);
?>
<?php $this->beginPage() ?>
.
. // Your code
Step - 3: If even though it didn't worked. Then,
Include
<?php
use app\assets\LoginAsset;
LoginAsset::register($this);
.
.
?>
on top of your view file.
Related Search
Two Different Layouts For Guest User & Logged In User-Yii2

How to get .js and .css files and replace them

I'm creating a Wordpress plugin.
I've created the basis for this and all is correctly set up.
I've tried looking at this and this to see if I could request and get all of the scripts (.js files) from the header and footer.
I thought that was the best function but the result is not happening right.
I basically want to get the JS and CSS files and replace with another string.
So let's say this is my page:
<!DOCTYPE html>
<html>
<head>
<title>Page</title>
<link rel="stylesheet" href="http://www.example.com/mycss.css" />
</head>
<body>
<script type="text/javascript" src="http://www.example.com/myscript.js"></script>
<script type="text/javascript" src="http://www.example.com/another_script.js"></script>
</body>
</html>
My function has to look for the .css and .js files and replace the string with other stuff. So that the result would be:
<!DOCTYPE html>
<html>
<head>
<title>Page</title>
<mytag>Here was contained a CSS from http://www.example.com/mycss.css</mytag>
</head>
<body>
<mytag>Here were contained 2 JS from http://www.example.com/myscript.js and http://www.example.com/another_script.js</mytag>
</body>
</html>
EDIT
I actually have to get the name and path for those files.
I think I can use a regexp but I don't know how to load it using get_header and get_footer (if those are the right functions)
It would make much more sense, rather than removing the relevant markup after receiving it from get_header() and get_footer() functions, to dequeue all styles and/or scripts so they are never printed there in the first place.
You can get the path for the file by looking back at the $wp_styles/$wp_scripts objects' registered property. You can then go on to get the filename using basename():
function dequeue_all_styles() {
global $wp_styles;
foreach( $wp_styles->queue as $handle ){
wp_dequeue_style($handle);
$src = $wp_styles->registered[$handle]->src;
$filename = basename($src);
}
}
add_action( 'wp_print_styles', 'dequeue_all_styles', 100 );
function dequeue_all_scripts() {
global $wp_scripts;
foreach( $wp_scripts->queue as $handle ){
wp_dequeue_script($handle);
$src = $wp_scripts->registered[$handle]->src;
$filename = basename($src);
}
}
add_action( 'wp_print_scripts', 'dequeue_all_scripts', 100 );
If you have other styles and scripts to put in their place, simply use wp_enqueue_style() and wp_enqueue_script().
You can access the enqueued script and styles by using $wp_scripts and $wp_styles global variables.
See the class reference for WP_Styles and WP_Scripts for available methods and properties.

Why does Kohana doesn't read my CSS?

My website is www.kipclip.com and it's running on Kohana. I created a new rental page. But it's not taking my CSS and JS files. I tried to find how this is included or if Kohana has a special method for do that. But still not successful. Do you have any idea regarding this?
A quick and dirty way to do so is to tack on the name of the script(s) and style(s) in the view you're implementing using regular html script and style tags and go on from there.
But, if you don't like quick and dirty, and prefer to do it better and more concrete, if you use Kohana 3.2, you can do the following. I haven't tried this on the older or newer versions, so it may or may not work in them (If you try to port it to that version, consult the transitioning document relative to the version in question that you wish to port):
/**
* /application/classes/controller/application.php
*/
abstract class Controller_Application extends Controller_Template {
public function before() {
parent::before();
if($this->auto_render) {
//Initialize empty values for use by ALL other derived classes
$this->template->site_name = '';//this is a psuedo-global set in this class
$this->template->title = '';//this too is set by the controller and action
$this->template->content = ''; //this is set by the controller and action
$this->template->styles = array();
$this->template->scripts = array();
$this->template->admin_scripts = array();
}
}
/**
* The after() method is called after your controller action.
* In our template controller we override this method so that we can
* make any last minute modifications to the template before anything
* is rendered.
*/
public function after()
{
if ($this->auto_render) {
//set the CSS files to include
$styles = array(
'style1', //the css file with all the defaults for the site
'jquery-library-css'
);
//set the JavaScript files to include
$scripts = array(
'myscript1',
'myscript2'
);
$admin_scripts = array(
'jquery-admin-functions',
);
//now, merge all this information into one so that it can be accessed
//by all derived classes:
$this->template->styles = array_merge($this->template->user_styles, $user_styles);
$this->template->scripts = array_merge($this->template->user_scripts, $user_scripts);
$this->template->admin_scripts = array_merge($this->template->admin_scripts, $admin_scripts);
}
//bind the site_name to the template view
$this->template->site_name = 'My Site Name';
//OLD WAY shown below:
View::set_global('site_name', 'My Site Name'); //set the site name
//now that everything has been set, use parent::after() to finish setting values
//and start rendering
parent::after();
}
}
So, how does this work? Remember that the application.php class is the base controller class from which all other controller classes are derived from. By implementing this type of binding to the base controller, every derived controller has access to what scripts, styles, etc. are available. And, as a result every associated view called by that controller also has access to those variables.
So, now to access those variables in your view:
Example, the template PHP file: /application/views/template.php
If it's defined like this (using PHP short tags - but do not use short tags in production code!):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html>
<head>
<meta charset='utf-8'/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<?php
/**
* Link the css files stored in the `static` folder from the project root.
* This will vary depending on how you have your files saved.
*/
foreach($user_styles as $style) : ?>
<link rel="stylesheet" href="<?php echo URL::base() . 'static/css/' . $style ?>.css" type="text/css"/>
<?php endforeach; ?>
<?php //Create AND set a dynamic page title - much like Facebook ?>
<title><?php echo $site_name; if(!empty($title)) echo ' - ' . $title; ?></title>
</head>
<body>
<!-- Fill in the body with HTML, PHP - whatever you want -->
<?php
/**
* Now, load the scripts:
* According to Yahoo, for better site performance, all scripts should be loaded after the body has been loaded
*/
foreach($user_scripts as $script) : ?>
<script src="<?php echo URL::base() . 'static/js/' . $script; ?>.js" type="text/javascript"></script>
<?php endforeach; ?>
</body>
</html>
There are two takeaway points from all of this:
One: If you want something to be global, or available to all controllers (and subsequent views), define them and bind them in the base application controller class.
Two: As a result, this functionality also gives you tremendous leverage and power in that if you have derived classes, you can them implement this same type of binding to that particular controller class making it available to any subsequent derived controller classes. That way, if you have two classes that should not have access to certain files and their associated functionality, e.g. an admin JavaScript file that loads all posts by some user, then this kind of implementation can make your life much, much, much easier.
And, a third hidden option is that give Kohana is PHP, you can use regular vanilla PHP / HTML in an associated view if you can't figure it out immediately. Although, that is something that I would dissuade you from doing in production code.
Whichever way, I hope this can assist you.

Body class trace for Joomla template like WordPress

How can I trace all the relevant tags of a page in the form of classes to the <body> tag? I'm referring to how Wordpress puts the following in a page's body class attribute:
home page page-id-12766 page-template page-template-page-home-php cufon2-disabled safari theme-*
This could be extremely helpful in Joomla template development.
Thanks in advance.
I don't know whether there is any out of the box solutions exists for this. As the main structure of the site is mainly based on the Menu structure, the below snippet may be useful. This is used in the templates/mytemplate/index.php file.
<?php
$menu = &JSite::getMenu();
$active = $menu->getActive();
$pageclass= '' ;
//$currentpage = ''; //In case if wanted to get the alias of the current page in a later stage.
if( count($active->tree) > 0 ) {
foreach ($active->tree as $key => $value) {
$pageclass .= 'level-'.$key . ' pageid-'.$value. ' page-'.$menu->getItem($value)->alias ;
//$currentpage = $menu->getItem($value)->alias;
}
}
?>
<body class="<?php echo $pageclass; ?>">
This will output something like:
<body class="level-0 pageid-101 page-home">
You may improve this using various values available in the $active variable.
Here's some information on the Body Class Function
How Wordpress determines which classes to apply using it is found in post-template.php
The relevant function you're looking for is get_body_class within this template. It essentially runs a series of checks to determine if the current page meets the criteria for given classes. It also relies heavily on Wordpress specific function calls that helps make that determination.

Insert code right after body tag using theme functions

I am trying to add a piece of code at the begining of every page in a Drupal site.
Since I have more than one page template, I want to do this programatically... but am not succeeding.
I am still new and, though I get the gist of hooks, theme functions, and the such, I just can't figure the correct way to achieve this.
So far I've overriden the theme_preprocess_page(&$vars) to add the necessary css and js:
function mytheme_preprocess_page(&$vars) {
if(condition) {
drupal_add_js(drupal_get_path('module', 'mymodule').'/js/modal.js');
}
}
How can I now add html code in every drupal page, preferably just after the opening bodytag or in any other starting section, via a function in the template.phpfile?
Thank you
In your preprocess function, any variable set, will be available in your page.tpl.php file.
function mytheme_preprocess_page(&$vars) {
if (condition) {
$vars['foo'] = '<div id="bar">TEST</div>';
}
}
then, in your page templates:
<body>
<?php print !empty($foo) ? $foo : ''; ?>
...
Had a look at https://www.drupal.org/project/google_tag. This is how they did it:
/**
* Implements hook_page_alter().
*
* Adds a post_render callback
*/
function MYMODULE_page_alter(&$page) {
$page['#post_render'][] = 'MYMODULE_CALLBACK';
}
/**
* Implements callback_post_render().
*
* Inserts JavaScript snippet immediately following the opening body tag.
*/
function MYMODULE_CALLBACK(&$children, $elements) {
$script = '<script type="text/javascript">console.log(\'hello world\');</script>';
// Insert snippet after the opening body tag.
$children = preg_replace('#<body[^>]*>#', '$0' . $script, $children, 1);
return $children;
}
This should keep you from having to modify any code in your templates:
function MY_MODULE_page_build(&$page)
{
$page['page_bottom']['my-markup'] = array('#markup' => '<div>My Markup Here</div>');
}
My approach finally was overriding the first rendered block in the page, in my case the Language Switcher.
Since I already was overriding it to customize it, it wasn't too much of a big deal, but it is anyway an ugly way to achieve that.
<?php
function mytheme_languageswitcher($links) {
// inserting this here like a virus in a dna strip
if(condition) {
drupal_add_js(drupal_get_path('module', 'mymodule').'/js/modal.js');
}
// the *real* code for the language switcher override
}
?>
Since the Language Switcher is rendered in every page, it works. The day the language switcher stops being displayed for whatever reason, this solution will FAIL.
You can add it to the footer with an option passed into the drupal_add_js function.
function THEMENAME_preprocess_page(&$variables) {
if (condition) {
drupal_add_js(drupal_get_path('theme', 'THEMENAME') . '/PATH/TO/FILE.js', array('scope' => 'footer'));
}
}
This will end up printing just before the closing body tag via the $page_bottom variable in the template html.tpl.php.
Another way to do it is to use the native drupal methods drupal_add_js and drupal_get_js.
// first, add your JS with a custom "scope" (before process phase)
<?php
drupal_add_js($tag_js, array(
'type' => 'inline',
'group' => JS_GROUP_TAGS,
'every_page' => TRUE,
'scope' => 'body_start',
'weight' => 1,
));
?>
// ugly way : add the drupal_get_js directly into html.tpl.php (but for testing, it's useful):
<body>
<?php print drupal_get_js('body_start'); ?>
...
</body>
// A cleaner way : use an intermediate variable, in a process method
// Exactly like template_process_html, in theme.inc
<?php
function MYMODULEORTHEME_process_html(&$variables){
$variables['js_body_start'] .= drupal_get_js('body_start');
}
?>
Enjoy :)

Resources