0

I have some sudo code here

    $image_position = left

    switch($image_position){
        case 'left':
            $block_con = "require(locate_template('templates/block-media-left.php'))";
            break;
        case 'right':
            $block_con = 'require(locate_template("templates/block-media-right.php"))';
            break;
        case 'center':
            $block_con = 'require(locate_template("templates/block-media-center.php"))';
            break;  
        default:
            $block_con = 'require(locate_template("templates/block-text.php"))';

    }


    <div>

        <?php echo $block_con; ?>

    </div>

Depending on the value of the $image_position I want to load a different block of php found in a templates folder.

The switch works but echo a string and does not add the require code.

I didn't think it would but don't know how to do this.

3
  • 3
    Why don t you just execute the require when you pass in the switch? And put the switch inside your div. Commented Oct 29, 2015 at 18:11
  • I think you need eval() but some guys say that it's evil. Commented Oct 29, 2015 at 18:13
  • 1
    Also your php balises around the switch are lacking, i doubt this code execute. Commented Oct 29, 2015 at 18:13

1 Answer 1

2

How about this?

<?php 
$image_position = 'left';
switch($image_position){
    case 'left':
        $block_con = 'templates/block-media-left.php';
        break;
    case 'right':
        $block_con = 'templates/block-media-right.php';
        break;
    case 'center':
        $block_con = 'templates/block-media-center.php';
        break;  
    default:
        $block_con = 'templates/block-text.php';
}
?>


<div>
    <?php require(locate_template($block_con)); ?>
</div>

It's important to note that this is just REQUIRING the PHP file currently. This means it'll be included and executed. You'd have to echo in that included PHP file to get any output.

Sign up to request clarification or add additional context in comments.

6 Comments

Still lacking php balises on the switch but yeah this one should work if you add them.
@LaurentFauvel Updated, thanks. That's what I get for copypasta :)
What in the world are php balises?
Sorry it's just sudo code, I have tags in the actual code
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.