2

Right now I have the game account number which is showing all the numbers, but what I want is to only show the last 4 numbers.I am pretty new to Symfony, so my question is how can I have this code in twig ? Or Do I have to do it the controller ? Thanks in advance...

So what I want is this, which is the the code I want in twig. My confusion is dispalying this code in the twig:

$gameId = '123456789';
$gameId = str_repeat('*', strlen($gameId) - 4) . substr($gameId, -4);

var_dump($gameId); //outputs  *****6789

this is what I already have, which is showing all the numbers that is being displayed from the form...

  <input value="{{ gameAccount.getAccountNumber() }}" 
    class="form-control" 
    dataid="{{ gameAccount.getId() }}" id="inputAccountNumber{{ gameAccount.getId() }}" 
    value="{# { gameAccount.getAccountNumber() } #}" 
    placeholder="Account Number" 
    type="text">

1 Answer 1

3

You can create a Twig Extension to do the job.

namespace AppBundle\Twig;

class AppExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('censorship', array($this, 'censorship')),
        );
    }

    public function censorship($id)
    {
        return str_repeat('*', strlen($id) - 4) . substr($id, -4);
    }

    public function getName()
    {
        return 'app_censorship';
    }
}

Then you register the extension as a service

# app/config/services.yml
services:
    app.twig_extension:
        class: AppBundle\Twig\AppExtension
        public: false
        tags:
            - { name: twig.extension }

and call it in you template like this

{{ gameAccount.getAccountNumber()|censorship }}

You can read more about it in the documentation.

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

2 Comments

I am using twig and do not have the services.yml. I installed twig via composer. Where would I tell twig to make the function available to my twig templates?
When you're using twig standalone, twig.symfony.com/doc/2.x/advanced.html#filters

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.