# Build different layouts from a flexible field

Source: https://uglydev.com/docs/how-do-i-read-elsewhere/build-different-layouts-from-a-flexible-field/
Updated: 20 November 2026

A **flexible content** field is a page built from blocks that the editor chooses and puts in any order: a hero, then some text, then three features. Your theme has to draw each block with its own template.

## The usual way

In ACF the loop keeps a hidden “current row”. Every template that runs inside the loop reads it with `get_sub_field()`, so it only works while the loop is going on.

```php
if ( have_rows( 'sections' ) ) :
	while ( have_rows( 'sections' ) ) : the_row();
		if ( get_row_layout() === 'hero' ) :
			get_template_part( 'parts/hero' );
		elseif ( get_row_layout() === 'text' ) :
			get_template_part( 'parts/text' );
		endif;
	endwhile;
endif;
```

And in `parts/hero.php`, to get the data:

```php
<h1><?php echo esc_html( get_sub_field( 'headline' ) ); ?></h1>
<?php if ( get_sub_field( 'image' ) ) : ?>
	<?php echo wp_get_attachment_image( get_sub_field( 'image' ), 'large' ); ?>
<?php endif; ?>
```

You choose the escape on every line, and the part cannot be used from anywhere else: there is no row to read outside the loop.

## With UglyFields

There is no hidden cursor. `ugly_rows()` gives a list, and each row is a variable you can pass anywhere. It knows its own `layout()`.

```php
foreach ( ugly_rows( 'sections' ) as $section ) {
	switch ( $section->layout() ) {
		case 'hero':
			get_template_part( 'parts/hero', null, array( 'row' => $section ) );
			break;
		case 'text':
			get_template_part( 'parts/text', null, array( 'row' => $section ) );
			break;
	}
}
```

And in `parts/hero.php`, the data comes from the row you were given:

```php
<?php $row = $args['row']; ?>
<h1><?php $row->show( 'headline' ); ?></h1>
<?php if ( $row->has( 'image' ) ) : ?>
	<?php $row->show( 'image' ); ?>
<?php endif; ?>
```

`show()` prints the value already escaped for its type, `get()` gives it to you, and `has()` says if there is something. A full example is [A landing page made of blocks](https://uglydev.com/recipes/a-landing-page-made-of-blocks/).
