Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an article explaining how to use Bower in Symfony #5159

Merged
merged 1 commit into from
May 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions cookbook/frontend/bower.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
.. index::
single: Front-end; Bower

Using Bower with Symfony
========================

Symfony and all its packages are perfectly managed by Composer. Bower is a
dependency management tool for front-end dependencies, like Bootstrap or
jQuery. As Symfony is purely a back-end framework, it can't help you much with
Bower. Fortunately, it is very easy to use!

Installing Bower
----------------

Bower_ is built on top of `Node.js`_. Make sure you have that installed and
then run:

.. code-block:: bash

$ npm install -g bower

After this command succeeded, run ``bower`` in your terminal to find out if
it's installed correctly.

.. tip::

If you don't want to have NodeJS on your computer, you can also use
BowerPHP_ (an unofficial PHP port of Bower). Beware that this is still in
an alpha state. If you're using BowerPHP, use ``bowerphp`` instead of
``bower`` in the examples.

Configuring Bower in your Project
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, see #5155 (comment)

---------------------------------

Normally, Bower downloads everything into a ``bower_components/`` directory. In
Symfony, only files in the ``web/`` directory are publicly accessible, so you
need to configure Bower to download things there instead. To do that, just
create a ``.bowerrc`` file with a new destination (like ``web/assets/vendor``):

.. code-block:: json

{
"directory": "web/assets/vendor/"
}

An Example: Installing Bootstrap
--------------------------------

Believe it or not, but you're now ready to use Bower in your Symfony
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this is necessary to say 👍 though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emphasing the easiness of using tools is never bad imo :)

application. As an example, you'll now install Bootstrap in your project and
include it in your layout.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mention perhaps where can they source it from bower.io/search url?


Installing the Dependency
~~~~~~~~~~~~~~~~~~~~~~~~~

To create a ``bower.json`` file, just run ``bower init``. Now you're ready to
start adding things to your project. For example, to add Bootstrap_ to your
``bower.json`` and download it, just run:

.. code-block:: bash

$ bower install --save bootstrap

This will install Bootstrap and its dependencies in ``web/assets/vendor/`` (or
whatever directory you configured in ``.bowerrc``).

.. seealso::

For more details on how to use Bower, check out `Bower documentation`_.

Including the Dependency in your Template
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now that the dependencies are installed, you can include bootstrap in your
template like normal CSS/JS:

.. configuration-block::

.. code-block:: html+jinja

{# app/Resources/views/layout.html.twig #}
<!doctype html>
<html>
<head>
{# ... #}

<link rel="stylesheet"
href="{{ asset('assets/vendor/bootstrap/dist/css/bootstrap.min.css') }}">
</head>

{# ... #}
</html>

.. code-block:: html+php

<!-- app/Resources/views/layout.html.php -->
<!doctype html>
<html>
<head>
{# ... #}

<link rel="stylesheet" href="<?php echo $view['assets']->getUrl(
'assets/vendor/bootstrap/dist/css/bootstrap.min.css'
) ?>">
</head>

{# ... #}
</html>

Great job! Your site is now using Bootstrap. You can now easily upgrade
bootstrap to the latest version and manage other front-end dependencies too.

.. _Bower: http://bower.io
.. _`Node.js`: https://nodejs.org
.. _BowerPHP: http://bowerphp.org/
.. _`Bower documentation`: http://bower.io/
.. _Bootstrap: http://getbootstrap.com/
7 changes: 7 additions & 0 deletions cookbook/frontend/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Front-end
=========

.. toctree::
:maxdepth: 2

bower
1 change: 1 addition & 0 deletions cookbook/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The Cookbook
email/index
event_dispatcher/index
form/index
frontend/index
logging/index
profiler/index
request/index
Expand Down
4 changes: 4 additions & 0 deletions cookbook/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@
* (validation) :doc:`/cookbook/validation/custom_constraint`
* (doctrine) :doc:`/cookbook/doctrine/file_uploads`

* :doc:`/cookbook/frontend/index`

* :doc:`/cookbook/frontend/bower`

* :doc:`/cookbook/logging/index`

* :doc:`/cookbook/logging/monolog`
Expand Down