Disable CSS Stylesheet for a Specific Action in Symfony

Disable CSS stylesheet for a specific action in Symfony

You can remove or add stylesheets to a modules view.yml by doing the following:

displaySuccess:
stylesheet: [-default]

would remove default.css from the display action. Simply putting

displaySuccess:
stylesheet: [-*]

would remove all stylesheets.

How do I disable inclusion of passed down JavaScript and Stylesheet files in view.yml for a module?

You can exclude all the passed down javascripts and stylesheets, or remove just specific ones.

For example:

indexSuccess:
stylesheet: [-style]

or:

indexSuccess:
stylesheet: [-*]

Including CSS in an email sent from a task in symfony

I ran into the same problem in my project. Here's how I fixed it:

  1. To keep the CSS separate but get it inline before sending the email, we use Emogrifier. Download the source code and put it into %sf_lib_dir%/vendor/emogrifier.

  2. Create a myMailer class that extends sfMailer. Mine is below. There are a few separate functions, but the key function is composeAndSendPartial, which takes a partial name (as a string), inserts all the CSS inline, and sends it. I tried to remove all code that's specific to my project, but I may have left some in. Let me know if it doesn't work for you or if you have any questions.

  3. In factories.yml, set the mailer: to myMailer

myMailer.class.php:

<?php
class myMailer extends sfMailer
{
/**
* Creates a new message with 2 bodies:
* * 1 with $body and MIME type text/html.
* * 1 with $body and tags stripped with MIME type text/plain. Stipped <br/>, </p>, and </div> tags and replaced with \n
*
* @param string|array $from The from address
* @param string|array $to The recipient(s)
* @param string $subject The subject
* @param string $body The body
*
* @return Swift_Message A Swift_Message instance
*/
public function composeAndSendHtml($from, $to, $subject, $body)
{
return $this->send($this->composeHtml($from, $to, $subject, $body));
}

/**
* Sends a message using composeHtml.
*
* @param string|array $from The from address
* @param string|array $to The recipient(s)
* @param string $subject The subject
* @param string $body The body
*
* @return int The number of sent emails
*/
public function composeHtml($from = null, $to = null, $subject = null, $body = null)
{
return Swift_Message::newInstance()
->setFrom($from)
->setTo($to)
->setSubject($subject)
->addPart($this->createPlainTextBody($body), 'text/plain')
->addPart($body, 'text/html');
}

/**
* Attempts to create a plaintext message with all html tags stripped out and new lines inserted as necessary
* @param $body
* @return $body
*/
public function createPlainTextBody($body)
{
$body = preg_replace('/\<br\s*\/?\>/i', "\n", $body); //replace all <br/s> with new lines
$body = preg_replace('/\<\/p\s*\>/i', "</p>\n\n", $body); //append 2 newlines to the end of each </p>
$body = preg_replace('/\<\/div\s*\>/i', "</div>\n\n", $body); //append 2 newlines to the end of each </div>
$body = strip_tags($body); //strip all tags from the body
return $body;
}

/**
* Composes and sends an email with a body from rendering $partial with $parameters
* @param string $from
* @param string $to
* @param string $subject
* @param string $partial the partial as a string. Feel free to change the default module name below
* @param array $parameters Parameters for the partial
* @param array $globalStylesheets The stylesheets that are included globally (usually global.css, maybe others)
*/
public function composeAndSendPartial($from, $to, $subject, $partial, $parameters = array(), $globalStylesheets = array())
{
require_once(sfConfig::get('sf_lib_dir') . '/vendor/emogrifier/emogrifier.php');

$context = sfContext::getInstance();
$response = $context->getResponse();
$originalStylesheets = $response->getStylesheets();

if (false !== $sep = strpos($partial, '/'))
{
$moduleName = substr($partial, 0, $sep);
$templateName = '_' . substr($partial, $sep + 1);
}
else
{
$moduleName = 'email';
$templateName = '_' . $partial;
}

sfConfig::set('sf_is_email', true);
$view = new sfPHPView($context, $moduleName, $templateName, ''); #not sure what 4th parameter does
$view->getAttributeHolder()->add($parameters);
$view->setDecorator(true);
$view->setDecoratorTemplate('email.php');
$html = $view->render();
sfConfig::set('sf_is_email', false);

$emailStylesheets = array_keys(array_diff_key($response->getStylesheets(), $originalStylesheets));

$css = '';
foreach($globalStylesheets as $globalStylesheet)
{
$css .= file_get_contents(sfConfig::get('sf_web_dir') . '/css/' . $globalStylesheet . '.css');
}
foreach ($emailStylesheets as $stylesheet)
{
$css .= file_get_contents(sfConfig::get('sf_web_dir') . '/css/' . $stylesheet . '.css');
$response->removeStylesheet($stylesheet);
}

$emog = new Emogrifier($html, $css);
$body = $emog->emogrify();

$this->composeAndSendHtml($from, $to, $subject, $body);
}
}

how to style a symfony2 form button

->add('submit', 'submit', array(
'label' => 'Radera denna video',
'attr' => array('class' => 'class-you-wish-to-add')
));

This is a duplicate of:
How to set a class attribute to a Symfony2 form input

Symfony Customize form errors css

You are rendering form fields using form_row widget which renders label, field & related error to do custom styling you can render your fields and their labels and errors individually like

{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register','novalidate': 'novalidate'}}) }}

{{ form_label(form.email) }}

<div class="some_class">{{ form_errors(form.email) }} </div>

{{ form_widget(form.email) }}

{{ form_end(form) }}

<style type="text/css">

.some_class{
/* write custom styling rules here */
}

</style>

Or get all errors in one place like

{# render any "global" errors #}
{{ form_errors(form) }}

Reference: Twig Template Form Function and Variable Reference

Add CSS to Form Type is Symfony2

Here is the way you can do it when building your form,

 $builder->add('field_name', 'text', array('label' => 'Field Label', 'attr' => array('class' => 'fieldClass')));

You can also do it when rendering your form fields, take a look at Twig template form function reference

{{ form_label(form.field, 'label', { 'attr': {'class': 'foo'} }) }}
{{ form_widget(form.field, { 'attr': {'class': 'bar'} }) }}

You can then add a css file in your bundle public assets and call it in your template using,

{% block stylesheets %}
{% parent() %} {# if you want to keep base template's stylesheets #}
<link href="{{ asset('bundle/myBundle/css/stylesheet.css') }}" type="text/css" rel="stylesheet" />
{% endblock %}

You've then to make your public assets be accessible through your web/ directory.
The best way to do it is to create symbolic links targeting your bundle public assets, you've then to execute

assets:install web/ --symlink

Another useful approach when you want to thoroughly customize specific form field rendering block (Twig) is to define a new form theme, here's the documentation > Form theming in Twig.

How is the force reload of javascript/css done in Symfony?

There is no built-in mechanism, but a little creativity means you can do this just about anywhere in your code, from view.yml to layout.php to each individual action.

The view.yml method is easy enough:

apps/frontend/config/view.yml:

  stylesheets:    [main?v=<?php echo time() ?>, reset?v=<?php echo time() ?>, layout?v=<?php echo time() ?>]

Although I think this is a little too active, and I tend to use either the SVN revision or a overall project version number:

  stylesheets:    [main?v=<?php echo sfConfig('app_project_version') ?>, reset?v=<?php echo sfConfig('app_project_version') ?>, layout?v=<?php echo sfConfig('app_project_version') ?>]

where app_project_version is set in apps/frontend/config/app.yml. Methods for layout.php and actionSuccess.php should be easy enough from here:

<?php use_stylesheet('blah?v='.sfConfig::get('app_project_version')); ?>

How do I include stylesheets with symfony properly?

This mainly because there are automatically loaded when you use a the admin template (see this file):

<?php if (isset($this->params['css']) && ($this->params['css'] !== false)): ?>
[?php use_stylesheet('<?php echo $this->params['css'] ?>', 'first') ?]
<?php elseif(!isset($this->params['css'])): ?>
[?php use_stylesheet('<?php echo sfConfig::get('sf_admin_module_web_dir').'/css/global.css' ?>', 'first') ?]
[?php use_stylesheet('<?php echo sfConfig::get('sf_admin_module_web_dir').'/css/default.css' ?>', 'first') ?]
<?php endif; ?>

This means you can define your own css in the generator.yml (and it won't load the default ones), like :

generator:
class: sfPropelGenerator
param:
css: /css/my_css.css

Or removed them:

generator:
class: sfPropelGenerator
param:
css: false

edit:

And finally you can remove them from the view.yml:

default:
http_metas:
content-type: text/html
stylesheets:
- -/sfPropelORMPlugin/css/global.css
- -/sfPropelORMPlugin/css/default.css
has_layout: true
layout: layout


Related Topics



Leave a reply



Submit