0

可能かどうかはわかりませんが、テンプレート内のifステートメントを使用できますか?

したがって、電話番号に値がない場合は、その文をまったく表示したくありません...

<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset="utf-8"/>
    <title>{form_title}</title>

</head>


<body>
    <p>You received the following message from {name} through the Gossip Cakes' contact form.</p>

    <p>Their email is {email}</p>

    <p>Their phone number is {phone}</p>

    <p>The message: {message}</p>

</body>


</html>

ストレートphpを使用できると思いますが、ビューのhtmlを返す方法はありますか?

4

3 に答える 3

2

クリエイティブな場合、CIテンプレートはIFステートメントをサポートしています。よく使います。それらを使用して、データがない場合にテンプレート要素が解析されないようにすることもできます。

次の3つの配列例を考えてみましょう。

$phone_numbers = array( 
                        array('phone_number'=>'555-1212'), 
                        array('phone_number'=>'555-1313') 
                      )

$phone_numbers = array()

$phone_numbers = array( array('phone_number'=>'555-1414') )

そして、これがあなたのhtmlの関連するセクションであると考えてください:

{phone_numbers} <p>{phone_number}</p> {phone_numbers}

3つの配列を使用すると、それぞれの出力配列1と3は次のようになります。(配列2を使用すると、制御配列が空であるため、何も出力されません。)

<p>555-1212</p><p>555-1313</p>

<p>555-1414</p>
于 2013-06-01T21:09:57.670 に答える
1

CIの組み込みパーサーを使用していると仮定すると、すべての変数を事前に準備する必要があります。現時点では、条件、変数の割り当て、またはループと基本的なトークンの置換以外のものはサポートされていません。

CIでこれを行うには、コントローラーで次のようなメッセージ全体を準備する必要があります。

if ($phone) {
    $data['phone_msg'] = "<p>Their phone number is $phone</p>";
} else {
    $data['phone_msg'] = '';
}

良い解決策ではありません。個人的には、素敵なテンプレートパーサーを探しているならTwigをお勧めします。「ストレートPHPを使用できると思います」というあなたの考えも非常に良いものです。

ビューのhtmlを返す方法はありますか?

view()次のようなの3番目のパラメータを使用します。

$html = $this->load->view('myview', $mydata, TRUE);
echo 'Here is the HTML:';
echo $html;
// OR...
echo $this->parser->parse_string($html, NULL, TRUE);
于 2012-10-12T21:21:02.817 に答える
0

クラスMY_ParserはCI_Parser{を拡張します

/**
 *  Parse a template
 *
 * Parses pseudo-variables contained in the specified template,
 * replacing them with the data in the second param, 
 * and clean the variables that have not been set
 *
 * @access  public
 * @param   string
 * @param   array
 * @param   bool
 * @return  string
 */
function _parse($template, $data, $return = FALSE)
{
    if ($template == '')
    {
        return FALSE;
    }

    foreach ($data as $key => $val)
    {
        if (is_array($val))
        {
            $template = $this->_parse_pair($key, $val, $template);
        }
        else
        {
            $template = $this->_parse_single($key, (string)$val, $template);
        }
    }

    #$template = preg_replace('/\{.*\}/','',$template);
    $patron = '/\\'.$this->l_delim.'.*\\'.$this->r_delim.'/';
    $template = preg_replace($patron,'',$template);
    if ($return == FALSE)
    {
        $CI =& get_instance();
        $CI->output->append_output($template);
    }

    return $template;
}

}

お楽しみください:D

于 2013-07-29T22:06:42.910 に答える