Skip to content

Reducing code sample line lengths to avoid horizontal scrolling #1975

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

Merged
merged 2 commits into from
Nov 26, 2012
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
36 changes: 27 additions & 9 deletions book/controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,13 @@ value to each variable.
object::

$httpKernel = $this->container->get('http_kernel');
$response = $httpKernel->forward('AcmeHelloBundle:Hello:fancy', array(
'name' => $name,
'color' => 'green',
));
$response = $httpKernel->forward(
'AcmeHelloBundle:Hello:fancy',
array(
'name' => $name,
'color' => 'green',
)
);

.. index::
single: Controller; Rendering templates
Expand All @@ -490,14 +493,20 @@ that's responsible for generating the HTML (or other format) for the controller.
The ``renderView()`` method renders a template and returns its content. The
content from the template can be used to create a ``Response`` object::

$content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
$content = $this->renderView(
'AcmeHelloBundle:Hello:index.html.twig',
array('name' => $name)
);

return new Response($content);

This can even be done in just one step with the ``render()`` method, which
returns a ``Response`` object containing the content from the template::

return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
return $this->render(
'AcmeHelloBundle:Hello:index.html.twig',
array('name' => $name)
);

In both cases, the ``Resources/views/Hello/index.html.twig`` template inside
the ``AcmeHelloBundle`` will be rendered.
Expand All @@ -517,15 +526,21 @@ The Symfony templating engine is explained in great detail in the
service. The ``templating`` service can also be used directly::

$templating = $this->get('templating');
$content = $templating->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
$content = $templating->render(
'AcmeHelloBundle:Hello:index.html.twig',
array('name' => $name)
);

.. note::

It is possible to render templates in deeper subdirectories as well, however
be careful to avoid the pitfall of making your directory structure unduly
elaborate::

$templating->render('AcmeHelloBundle:Hello/Greetings:index.html.twig', array('name' => $name));
$templating->render(
'AcmeHelloBundle:Hello/Greetings:index.html.twig',
array('name' => $name)
);
// index.html.twig found in Resources/views/Hello/Greetings is rendered.

.. index::
Expand Down Expand Up @@ -642,7 +657,10 @@ For example, imagine you're processing a form submit::
if ($form->isValid()) {
// do some sort of processing

$this->get('session')->setFlash('notice', 'Your changes were saved!');
$this->get('session')->setFlash(
'notice',
'Your changes were saved!'
);

return $this->redirect($this->generateUrl(...));
}
Expand Down
11 changes: 8 additions & 3 deletions book/doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,9 @@ on its ``id`` value::
->find($id);

if (!$product) {
throw $this->createNotFoundException('No product found for id '.$id);
throw $this->createNotFoundException(
'No product found for id '.$id
);
}

// ... do something, like pass the $product object into a template
Expand Down Expand Up @@ -555,7 +557,9 @@ you have a route that maps a product id to an update action in a controller::
$product = $em->getRepository('AcmeStoreBundle:Product')->find($id);

if (!$product) {
throw $this->createNotFoundException('No product found for id '.$id);
throw $this->createNotFoundException(
'No product found for id '.$id
);
}

$product->setName('New product name!');
Expand Down Expand Up @@ -1306,7 +1310,8 @@ and ``nullable``. Take a few examples:

/**
* A string field with length 255 that cannot be null
* (reflecting the default values for the "type", "length" and *nullable* options)
* (reflecting the default values for the "type", "length"
* and *nullable* options)
*
* @ORM\Column()
*/
Expand Down
4 changes: 3 additions & 1 deletion book/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,9 @@ method to specify the option::
{
$collectionConstraint = new Collection(array(
'name' => new MinLength(5),
'email' => new Email(array('message' => 'Invalid email address')),
'email' => new Email(
array('message' => 'Invalid email address')
),
));

return array('validation_constraint' => $collectionConstraint);
Expand Down
20 changes: 16 additions & 4 deletions book/from_flat_php_to_symfony2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,10 @@ them for you. Here's the same sample application, now built in Symfony2::
->createQuery('SELECT p FROM AcmeBlogBundle:Post p')
->execute();

return $this->render('AcmeBlogBundle:Blog:list.html.php', array('posts' => $posts));
return $this->render(
'AcmeBlogBundle:Blog:list.html.php',
array('posts' => $posts)
);
}

public function showAction($id)
Expand All @@ -570,7 +573,10 @@ them for you. Here's the same sample application, now built in Symfony2::
throw $this->createNotFoundException();
}

return $this->render('AcmeBlogBundle:Blog:show.html.php', array('post' => $post));
return $this->render(
'AcmeBlogBundle:Blog:show.html.php',
array('post' => $post)
);
}
}

Expand All @@ -590,7 +596,10 @@ now quite a bit simpler:
<ul>
<?php foreach ($posts as $post): ?>
<li>
<a href="<?php echo $view['router']->generate('blog_show', array('id' => $post->getId())) ?>">
<a href="<?php echo $view['router']->generate(
'blog_show',
array('id' => $post->getId())
) ?>">
<?php echo $post->getTitle() ?>
</a>
</li>
Expand All @@ -605,7 +614,10 @@ The layout is nearly identical:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $view['slots']->output('title', 'Default title') ?></title>
<title><?php echo $view['slots']->output(
'title',
'Default title'
) ?></title>
</head>
<body>
<?php echo $view['slots']->output('_content') ?>
Expand Down
14 changes: 11 additions & 3 deletions book/page_creation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,16 @@ of writing the HTML inside the controller, render a template instead:
{
public function indexAction($name)
{
return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
return $this->render(
'AcmeHelloBundle:Hello:index.html.twig',
array('name' => $name)
);

// render a PHP template instead
// return $this->render('AcmeHelloBundle:Hello:index.html.php', array('name' => $name));
// return $this->render(
// 'AcmeHelloBundle:Hello:index.html.php',
// array('name' => $name)
// );
}
}

Expand Down Expand Up @@ -902,7 +908,9 @@ file of your choice::
// app/AppKernel.php
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
$loader->load(
__DIR__.'/config/config_'.$this->getEnvironment().'.yml'
);
}

You already know that the ``.yml`` extension can be changed to ``.xml`` or
Expand Down
56 changes: 30 additions & 26 deletions book/propel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ persist it to the database and fetch it back out.
.. sidebar:: Code along with the example

If you want to follow along with the example in this chapter, create an
``AcmeStoreBundle`` via:
``AcmeStoreBundle`` via:

.. code-block:: bash

$ php app/console generate:bundle --namespace=Acme/StoreBundle
Expand Down Expand Up @@ -171,19 +171,21 @@ Fetching Objects from the Database
Fetching an object back from the database is even easier. For example, suppose
you've configured a route to display a specific ``Product`` based on its ``id``
value::

// ...
use Acme\StoreBundle\Model\ProductQuery;

public function showAction($id)
{
$product = ProductQuery::create()
->findPk($id);

if (!$product) {
throw $this->createNotFoundException('No product found for id '.$id);
throw $this->createNotFoundException(
'No product found for id '.$id
);
}

// ... do something, like pass the $product object into a template
}

Expand All @@ -192,22 +194,24 @@ Updating an Object

Once you've fetched an object from Propel, updating it is easy. Suppose you
have a route that maps a product id to an update action in a controller::

// ...
use Acme\StoreBundle\Model\ProductQuery;

public function updateAction($id)
{
$product = ProductQuery::create()
->findPk($id);

if (!$product) {
throw $this->createNotFoundException('No product found for id '.$id);
throw $this->createNotFoundException(
'No product found for id '.$id
);
}

$product->setName('New product name!');
$product->save();

return $this->redirect($this->generateUrl('homepage'));
}

Expand All @@ -227,12 +231,12 @@ method on the object::

Querying for Objects
--------------------

Propel provides generated ``Query`` classes to run both basic and complex queries
without any work::

\Acme\StoreBundle\Model\ProductQuery::create()->findPk($id);

\Acme\StoreBundle\Model\ProductQuery::create()
->filterByName('Foo')
->findOne();
Expand Down Expand Up @@ -287,13 +291,13 @@ Start by adding the ``category`` definition in your ``schema.xml``:
<column name="name" type="varchar" primaryString="true" size="100" />
<column name="price" type="decimal" />
<column name="description" type="longvarchar" />

<column name="category_id" type="integer" />
<foreign-key foreignTable="category">
<reference local="category_id" foreign="id" />
</foreign-key>
</table>

<table name="category">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
<column name="name" type="varchar" primaryString="true" size="100" />
Expand Down Expand Up @@ -326,23 +330,23 @@ Now, let's see the code in action. Imagine you're inside a controller::
use Acme\StoreBundle\Model\Category;
use Acme\StoreBundle\Model\Product;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{
public function createProductAction()
{
$category = new Category();
$category->setName('Main Products');

$product = new Product();
$product->setName('Foo');
$product->setPrice(19.99);
// relate this product to the category
$product->setCategory($category);

// save the whole
$product->save();

return new Response(
'Created product id: '.$product->getId().' and category id: '.$category->getId()
);
Expand All @@ -363,15 +367,15 @@ before. First, fetch a ``$product`` object and then access its related

// ...
use Acme\StoreBundle\Model\ProductQuery;

public function showAction($id)
{
$product = ProductQuery::create()
->joinWithCategory()
->findPk($id);

$categoryName = $product->getCategory()->getName();

// ...
}

Expand All @@ -395,7 +399,7 @@ inserted, updated, deleted, etc).
To add a hook, just add a new method to the object class::

// src/Acme/StoreBundle/Model/Product.php

// ...
class Product extends BaseProduct
{
Expand Down
Loading