ccc

Symfony: Heredando JS y CSS en las vistas

Crear src\AppBundle\Controller\HolaController.php
<?php
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class HolaController extends Controller
{
    /**
     * @Route("/hola", name="hola")
     */
    public function indexAction(Request $request)
    {
        // replace this example code with whatever you need
        return $this->render('hola/hola.twig', array(
            'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
        ));
    }
}

Modificar app\Resources\views\base.html.twig:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>{% block title %}Welcome!{% endblock %}</title>
        {% block stylesheets %}{% endblock %}
        <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
    </head>
    <body>
        {% block body %}{% endblock %}

{% block csslsg %}
<link rel="stylesheet" href="{{asset('/css/style.css')}}">
{% endblock %}

        {% block jslsg %}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
{% endblock %}
    </body>
</html>

Crear app\Resources\views\hola\hola.twig:
{% extends 'base.html.twig' %}

{% block body %}
    <div id="wrapper">
        <div id="container">
   <p>La ruta es: {{ base_dir }}</p>
   </div>

    </div>
{% endblock %}

{% block csslsg %}
{{parent()}}
<link rel="stylesheet" href="{{asset('/css/substyle.css')}}">
{% endblock %}

{% block jslsg %}
{{parent()}}
<script type="text/javascript" src="{{asset('/js/prueba.js')}}"></script>
{% endblock %}

Crear web\css\style.css:
body { background-color:#FF0000;}

Crear web\css\substyle.css:
p { font-size:30px;color:#FFF; }

Crear web\js\prueba.js:
$(function() {
alert("carga bien");
});


No hay comentarios:

Publicar un comentario