1

I want to create some YAML key with value - the code in php. But this code shouldn't be executed - I want to use it as the value itself. How can I do this?

Example:

somekey: <?php echo 1; ?>

This line shouldn't be recognized as somekey: 1 after parsing - I want to receive value for somekey key as <?php echo 1; ?>.

Update:

I want to use this to store some code in database (with fixtures.yml) and then load it and use as php-based template.

1
  • 1
    Long shot... somekey: <?php echo '<?php echo 1; ?>' ?> Commented Dec 20, 2010 at 23:07

5 Answers 5

2

I invented my own solution. It looks badly but works:

somekey: "<?php echo '<?php echo 1; ?>'; ?>" 
Sign up to request clarification or add additional context in comments.

Comments

1

You could store it as:

somekey: [?php echo 1; ?]

and replace the [?php ?] with appropriate php tags just before you need to use the template (the layer which loads the yaml could do the work).

Comments

1

i've been using fixtures for migrations purpose and also needed to use php to generate the proper yaml file. My work around this problem was doing something like:

<?php $handle=fopen(sfConfig::get('sf_data_dir')."/fixtures/csv/file.csv","r")?>
<?php fgetcsv($handle,0, ";"); ?>
<?php $i=100; ?>
<?php while (($data = fgetcsv($handle,0, ";")) !== false):?>
<?php
$code=trim($data[0]);
$user=trim($data[1]);
$criteria = new Criteria();
$criteria->add(ObjectPeer::CODE, $code);
$unit= objectPeer::doSelectOne($criteria, $error);
if (is_null($unit) || empty($unit))
  continue;
?>
  auto_generated_label_<?php echo 'value'.$i; $i++?>:

    attribute_1: <?php echo $user ?>

    attribute_2: <?php echo $unit->getId() ?>

<?php endwhile ?>

So in symfony 1.2 this is working nicely, and remember, always leave a blank like between each yaml entity like in the example, and respect indentation when dealing with php embebbed on a yaml file, because those blank lines will not be there when the yaml parse reads it, so if you not put them, you could end up with somethink like:

auto_generated_label_1: attribute_1: asdasdasd attribute_2: 1233123

Best of luck!

Comments

0

You haven't specified when (if ever) you want the php code to actually execute. So assuming that you never want it to execute and you're outputting the value to a browser somewhere, try &lt;?php echo 1; ?&gt; (or HTML escaping whatever the actual value is)

Comments

0

Symfony 1.0:

version: <?php echo file_get_contents('1.0/VERSION')."\n" ?>

Symfony 1.x:

version: "<?php echo file_get_contents('1.1/VERSION') ?>"

Comments

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.