Ir para conteúdo
Fórum Script Brasil
  • 0

templates


Rudimar

Pergunta

peguei um exemplo de arquivos templates aqui no forum, só que não está dando muito certo.

É o seguinte: eu executo um arquivo php que contem algumas variaveis. Os valores destas variaveis serão exportadas para um

arquivo .doc do Office. Está funcionando parcialmente.

os problemas são os seguintes:

1 - eu criei um arquivo doc e coloquei neste arquivo uma tabela e algumas formatações. Tudo muito simples, uma tabela com apenas duas colunas e duas linhas com alguma cor nas bordas da tabela. Ocorre que ao executar o script, o word ao invés de interpretar esta tabela ela mostra o codigo fonte como as tags, os comandos html, eu gostaria que exibisse os resultados destas tags e htmls. Acho que deu para entender, explicando melhor, ao invés de mostrar o resultado está mostrando o codigo fonte, por assim dizer.

2 - Segundo o manual que li, o IE deveria mostrar o arquivo doc no browser (acho que já visto em algum lugar na internet), só que ao invés disto ele está baixando o arquivo doc, o Word é carregado e é mostrado o arquivo em questao. Por que o arquivo doc não está sendo mostrado no browser?

Bem abaixo os codigos:

O primeiro codigo abaixo é na realidade um arquivo doc que criei, onde coloquei a tabela mencionada no item 1 acima. Segundo o manual que li, eu deveria após criar o arquivo doc salvar este arquivo como html, e foi o que fiz, e gerou o codigo abaixo, este é o arquivo relatorio.html.

<HTML>

<HEAD>

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">

<META NAME="Generator" CONTENT="Microsoft Word 97">

<TITLE>Olá {FULANO}, com templates PHP</TITLE>

</HEAD>

<BODY>

<TABLE BORDER CELLSPACING=2 BORDERCOLOR="#008000" CELLPADDING=4 WIDTH=598>

<TR><TD WIDTH="50%" VALIGN="TOP">

<FONT SIZE=2><P>Ol&aacute; {FULANO}, com templates PHP!</FONT></TD>

<TD WIDTH="50%" VALIGN="TOP">

<FONT SIZE=2><P>Ol&aacute; {FULANO2}, com templates PHP!</FONT></TD>

</TR>

<TR><TD WIDTH="50%" VALIGN="TOP">&nbsp;</TD>

<TD WIDTH="50%" VALIGN="TOP">&nbsp;</TD>

</TR>

</TABLE>

<FONT SIZE=2></FONT></BODY>

</HTML>

Aqui eu tenho o arquivo templa.php:

<?php

require("Template.class.php");

// Forçando o cabeçalho para o formato escolhido do Office

header('Content-type: application/msword');

header('Content-Disposition: attachment; filename="Relatorio.doc"');

header("Pragma: no-cache");

header("Expires: 0");

// Arquivo relatorio.html, gerado no Word

$tpl = new Template("relatorio.htm",true);

// Variáveis, blocos, etc

$tpl->FULANO = "Rael";

$tpl->FULANO2 = "Rudimar";

$tpl->show();

?>

e finalmente abaixo o arquivo template.

<?php

/**

* Mecanismo de Template para PHP5

*

* Mecanismos de Template permitem manter o código HTML em arquivos externos

* que ficam completamente livres de código PHP. Dessa forma, consegue-se manter

* a lógica de programação (PHP) separada da estrutura visual (HTML ou XML, CSS, etc).

*

* Se você já é familiar ao uso de mecanismos de template PHP, esta classe inclui

* algumas melhorias: suporte à objetos, automaticamente detecta blocos, mantém uma

* lista interna das variáveis que existem, limpa automaticamente blocos "filhos",

* avisando quando tentamos chamar blocos ou variáveis que são existem, avisando quando

* criamos blocos "mal formados", e outras pequenas ajudas.

*

* @author Rael G.C. (rael.gc@gmail.com)

* @version 1.5

*/

class Template {

/**

* A list of existent document variables.

*

* @var array

*/

private $vars = array();

/**

* A hash with vars and values setted by the user.

*

* @var array

*/

private $values = array();

/**

* A hash of existent object properties variables in the document.

*

* @var array

*/

private $properties = array();

/**

* A hash of the object instances setted by the user.

*

* @var array

*/

private $instances = array();

/**

* A list of all automatic recognized blocks.

*

* @var array

*/

private $blocks = array();

/**

* A list of all blocks that contains at least a "child" block.

*

* @var array

*/

private $parents = array();

/**

* Describes the replace method for blocks. See the Template::setFile()

* method for more details.

*

* @var boolean

*/

private $accurate;

/**

* Regular expression to find var and block names.

* Only alfa-numeric chars and the underscore char are allowed.

*

* @var string

*/

private static $REG_NAME = "([[:alnum:]]|_)+";

/**

* Cria um novo template, usando $filename como arquivo principal

*

* Quando o parâmetro $accurate é true, a substituição dos blocos no arquivo

* final será perfeitamente fiel ao arquivo original, isto é, todas as tabulações

* serão removidas. Isso vai ter um pequeno prejuízo na performance, que pode variar

* de acordo com a versão do PHP em uso. Mas é útil quando estamos usando tags HTML

* como &lt;pre&gt; ou &lt;code&gt;. Em outros casos, ou melhor, quase sempre,

* nunca se mexe no valor de $accurate.

*

* @param string $filename caminho do arquivo que será lido

* @param booelan $accurate true para fazer substituição fiel das tabulações

*/

public function __construct($filename, $accurate = false){

$this->accurate = $accurate;

$this->loadfile(".", $filename);

}

/**

* Adiciona o conteúdo do arquivo identificado por $filename na variável de template

* identificada por $varname

*

* @param string $varname uma variável de template existente

* @param string $filename arquivo a ser carregado

*/

public function addFile($varname, $filename){

if(!in_array($varname, $this->vars)) throw new Exception("addFile: var $varname não existe");

$this->loadfile($varname, $filename);

}

/**

* Não use este método, ele serve apenas para podemos acessar as variáveis

* de template diretamente.

*

* @param string $varname template var name

* @param mixed $value template var value

*/

public function __set($varname, $value){

if(!in_array($varname, $this->vars)) throw new Exception("var $varname não existe");

$stringValue = $value;

if(is_object($value)){

$this->instances[$varname] = $value;

if(!array_key_exists($varname, $this->properties)) $this->properties[$varname] = array();

if(method_exists($value, "__toString")) $stringValue = $value->__toString();

else $stringValue = "Object";

}

$this->setValue($varname, $stringValue);

return $value;

}

/**

* Não use este método, ele serve apenas para podemos acessar as variáveis

* de template diretamente.

*

* @param string $varname template var name

*/

public function __get($varname){

if (isset($this->values["{".$varname."}"])) return $this->values["{".$varname."}"];

throw new Exception("var $varname não existe");

}

/**

* Loads a file identified by $filename.

*

* The file will be loaded and the file's contents will be assigned as the

* variable's value.

* Additionally, this method call Template::recognize() that identifies

* all blocks and variables automatically.

*

* @param string $varname contains the name of a variable to load

* @param string $filename file name to be loaded

*

* @return void

*/

private function loadfile($varname, $filename) {

if (!file_exists($filename)) throw new Exception("arquivo $filename não existe");

$file_array = file($filename);

$blocks = $this->recognize($file_array, $varname);

$str = implode("", $file_array);

if (empty($str)) throw new Exception("arquivo $filename está vazio");

$this->setValue($varname, $str);

$this->createBlocks($blocks);

}

/**

* Identify all blocks and variables automatically and return them.

*

* All variables and blocks are already identified at the moment when

* user calls Template::setFile(). This method calls Template::identifyVars()

* and Template::identifyBlocks() methods to do the job.

*

* @param array $file_array contains all lines from the content file

* @param string $varname contains the variable name of the file

*

* @return array an array where the key is the block name and the value is an

* array with the children block names.

*/

private function recognize(&$file_array, $varname){

$blocks = array();

$queued_blocks = array();

foreach ($file_array as $line) {

if (strpos($line, "{")!==false) $this->identifyVars($line);

if (strpos($line, "<!--")!==false) $this->identifyBlocks($line, $varname, $queued_blocks, $blocks);

}

return $blocks;

}

/**

* Identify all user defined blocks automatically.

*

* @param string $line contains one line of the content file

* @param string $varname contains the filename variable identifier

* @param string $queued_blocks contains a list of the current queued blocks

* @param string $blocks contains a list of all identified blocks in the current file

*

* @return void

*/

private function identifyBlocks(&$line, $varname, &$queued_blocks, &$blocks){

$reg = "/<!--\s*BEGIN\s+(".self::$REG_NAME.")\s*-->/sm";

preg_match($reg, $line, $m);

if (1==preg_match($reg, $line, $m)){

if (0==sizeof($queued_blocks)) $parent = $varname;

else $parent = end($queued_blocks);

if (!isset($blocks[$parent])){

$blocks[$parent] = array();

}

$blocks[$parent][] = $m[1];

$queued_blocks[] = $m[1];

}

$reg = "/<!--\s*END\s+(".self::$REG_NAME.")\s*-->/sm";

if (1==preg_match($reg, $line)) array_pop($queued_blocks);

}

/**

* Identifies all variables defined in the document.

*

* @param string $line contains one line of the content file

*/

private function identifyVars(&$line){

$reg = "/{(".self::$REG_NAME.")(\-\>)?(".self::$REG_NAME.")?}/";

$r = preg_match_all($reg, $line, $m);

if ($r>0){

for($i=0; $i<$r; $i++){

// Object var detected

if($m[4][$i]) $this->properties[$m[1][$i]][] = $m[4][$i];

$this->vars[] = $m[1][$i];

}

}

}

/**

* Create all identified blocks given by Template::identifyBlocks().

*

* @param array $blocks contains all identified block names

* @return void

*/

private function createBlocks(&$blocks) {

$this->parents = array_merge($this->parents, $blocks);

foreach($blocks as $parent => $block){

foreach($block as $chield){

if(in_array($chield, $this->blocks)) throw new Exception("bloco duplicado: $chield");

$this->blocks[] = $chield;

$this->setBlock($parent, $chield);

}

}

}

/**

* A variable $parent may contain a variable block defined by:

* &lt;!-- BEGIN $varname --&gt; content &lt;!-- END $varname --&gt;.

*

* This method removes that block from $parent and replaces it with a variable

* reference named $block. The block is inserted into the varKeys and varValues

* hashes.

* Blocks may be nested.

*

* @param string $parent contains the name of the parent variable

* @param string $block contains the name of the block to be replaced

* @return void

*/

private function setBlock($parent, $block) {

$name = "B_".$block;

$str = $this->getVar($parent);

if($this->accurate){

$str = str_replace("\r\n", "\n", $str);

$reg = "/\t*<!--\s*BEGIN\s+$block\s+-->\n*(\s*.*?\n?)\t*<!--\s+END\s+$block\s*-->\n?/sm";

}

else $reg = "/<!--\s*BEGIN\s+$block\s+-->\n*(\s*.*?\n?)<!--\s+END\s+$block\s*-->\n?/sm";

if(1!==preg_match($reg, $str, $m)) throw new Exception("bloco $block está mal formado");

$this->setValue($name, '');

$this->setValue($block, $m[1]);

$this->setValue($parent, preg_replace($reg, "{".$name."}", $str));

}

/**

* Internal setValue() method.

*

* The main difference between this and Template::__set() method is this

* method cannot be called by the user, and can be called using variables or

* blocks as parameters.

*

* @param string $varname constains a varname

* @param string $value constains the new value for the variable

* @return void

*/

private function setValue($varname, $value) {

$this->values["{".$varname."}"] = $value;

}

/**

* Returns the value of the variable identified by $varname.

*

* @param string $varname the name of the variable to get the value of

* @return string the value of the variable passed as argument

*/

private function getVar($varname) {

return $this->values['{'.$varname.'}'];

}

/**

* Limpa o valor de uma variável

*

* O mesmo que $this->setValue($varname, "");

*

* @param string $varname nome da variável

*/

public function clearVar($varname) {

$this->setValue($varname, "");

}

/**

* Fill in all the variables contained within the variable named

* $varname. The resulting value is returned as the function result and the

* original value of the variable varname is not changed. The resulting string

* is not "finished", that is, the unresolved variable name policy has not been

* applied yet.

*

* @param string $varname the name of the variable within which variables are to be substituted

* @return string the value of the variable $varname with all variables substituted.

*/

private function subst($varname) {

$s = $this->getVar($varname);

// Common variables replacement

$s = str_replace(array_keys($this->values), $this->values, $s);

// Object variables replacement

foreach($this->instances as $varname => $inst) foreach($this->properties[$varname] as $p){

// Non boolean accessor

$method = str_replace('_', '', $p);

if(method_exists($inst, "get$method")) $s = str_replace("{".$varname.'->'.$p."}", $inst->{"get".$method}(), $s);

// Boolean accessor

elseif(method_exists($inst, "is$method")) $s = str_replace("{".$varname.'->'.$p."}", $inst->{"is".$method}(), $s);

else throw new Exception("não existe método na classe ".get_class($inst)." para acessar ".$varname."->".$p);

}

return $s;

}

/**

* Clear all child blocks of a given block.

*

* @param string $block a block with chield blocks.

*/

private function clearBlocks($block) {

if (isset($this->parents[$block])){

$chields = $this->parents[$block];

foreach($chields as $chield){

$this->clearVar("B_".$chield);

}

}

}

/**

* Mostra um bloco.

*

* Esse método deve ser chamado quando um bloco deve ser mostrado.

* Sem isso, o bloco não irá aparecer no conteúdo final.

*

* Se o parâmetro $append for true, o conteúdo do bloco será

* adicionado ao conteúdo que já existia antes. Ou seja, use true

* quando quiser que o bloco seja duplicado.

*

* @param string $block nome do bloco que deve ser mostrado

* @param boolean $append true se o conteúdo anterior deve ser mantido (ou seja, para duplicar o bloco)

*/

public function parseBlock($block, $append = false) {

if(!in_array($block, $this->blocks)) throw new Exception("bloco $block não existe");

if ($append) $this->setValue("B_".$block, $this->getVar("B_".$block) . $this->subst($block));

else $this->setValue("B_".$block, $this->subst($block));

$this->clearBlocks($block);

}

/**

* Retorna o conteúdo final, sem mostrá-lo na tela.

* Se você quer mostrá-lo na tela, use o método Template::show().

*

* @return string

*/

public function getContent() {

// After subst, remove empty vars

return preg_replace("/{(".self::$REG_NAME.")(\-\>)?(".self::$REG_NAME.")?}/", "", $this->subst("."));

}

/**

* Mostra na tela o conteúdo final.

*/

public function show() {

echo $this->getContent();

}

}

?>

Link para o comentário
Compartilhar em outros sites

10 respostass a esta questão

Posts Recomendados

  • 0
1 - O word não interpreta HTML... logo, ele vai mostrar os códigos mesmo.
mas veja o artigo que peguei no link http://www.raelcunha.com/template.php#office_files, no item Criando arquivos do Office, diz o seguinte:

Se você precisa elaborar um relatório que deve ser exibido em formato do Word (.doc) ou do Excel (.xls), também podemos usar a classe Template para isso.

Em primeiro lugar, crie normalmente no Office seu relatório. Após terminar, escolha a opção "Salvar como", e selecione o formato HTML. Feito isso, abra este arquivo HTML em seu editor PHP (não se assuste, é bastante poluído e cheio de tags estranhas) e use-o conforme visto até agora: crie variáveis, declare blocos, nada de diferente.

Se você for salvar o conteúdo em um arquivo, coloque neste arquivo a extensão .doc" (ou .xls no caso de uma planilha). O Office abrirá normalmente este arquivo, convertendo-o automaticamente de HTML para o formato desejado na primeira vez em que for aberto.

Se você for exibir o conteúdo no navegador ao invés de salvá-lo num arquivo, você precisa modificar o header para avisar o navegador que se trata de um documento do Office, forçando o navegador à interpretá-lo como tal (o Firefox irá fazer o download do arquivo, o IE irá abrir o Microsoft Office como um plugin e exibir o arquivo dentro do navegador mesmo). Faça isso com a instrução header() do PHP:

Note o texto que coloquei em negrito, pelo o que entendi dá para salvar o arquivo que está num formato html para doc... ou será que estou entendendo errado?

Bem, o que procuro é, ao executar um script php, vou gerar uma planilha com script php e quero salvar esta planinha num arquivo doc...

Link para o comentário
Compartilhar em outros sites

  • 0

Realmente foi interpretado... tem que ver se você está colocando todas as tags, testei no Office 2003.

Voce quis dizer que não foi interpretado né?

Aquele codigo que voce mostrou é o arquivo original ou é o arquivo final (que foi feito o download)?

Enfim, funcionaou para voce? Talvez seja problema no meu "bom" e velho windows 98.

Link para o comentário
Compartilhar em outros sites

  • 0

Foi interpretado.. funcionou, mas só funciona quando se usa as tags (note que eu montei o código na mão, não usei nada do site que você passou). se colocar <b>eeeeee</b> apenas, ai não funciona... enfim, testei com o Office 2003, a versão do Windows não acredito que interfira nisso, posto que, quem interpreta é o Word e não o Windows...

Link para o comentário
Compartilhar em outros sites

  • 0

Sim, foi, as partes em negrito aparecem em negro... o que retornou foi:

Fatal error: Uncaught exception 'Exception' with message 'var FULANO não existe' in /home/negocios/public_html/diario/folha_pagamento/Template.class.php:116 Stack trace: #0 /home/negocios/public_html/diario/folha_pagamento/templa.php(27): Template->__set('FULANO', 'Rael') #1 {main} thrown in /home/negocios/public_html/diario/folha_pagamento/Template.class.php on line 116

Link para o comentário
Compartilhar em outros sites

  • 0

putz, tinha um negocio errado aqui, não era para aparecer todos aqueles erros, agora corrigi, poderia acessar o link abaixo novamente:

http://www.negocios.online.nom.br/diario/t...ates/templa.php

para mim aparece isto aqui:

<HTML>

<HEAD>

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">

<META NAME="Generator" CONTENT="Microsoft Word 97">

<TITLE>Olá Rael, com templates PHP</TITLE>

</HEAD>

<BODY>

<TABLE BORDER CELLSPACING=2 BORDERCOLOR="#008000" CELLPADDING=4 WIDTH=598>

<TR><TD WIDTH="50%" VALIGN="TOP">

<FONT SIZE=2><P>Ol&aacute; Rael, com templates PHP!</FONT></TD>

<TD WIDTH="50%" VALIGN="TOP">

<FONT SIZE=2><P>Ol&aacute; Rudimar, com templates PHP!</FONT></TD>

</TR>

<TR><TD WIDTH="50%" VALIGN="TOP">&nbsp;</TD>

<TD WIDTH="50%" VALIGN="TOP">&nbsp;</TD>

</TR>

</TABLE>

<FONT SIZE=2></FONT></BODY>

</HTML>

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      651,9k
×
×
  • Criar Novo...