2012-12-07 2 views
0

나는 HTML 문서의 태그가 지정된 영역을 대체하는 도구로 작업하고 있습니다. 몇 가지 PHP 템플리트 시스템을 살펴 보았습니다. 그러나 실제로 찾고자하는 것이 아니므로 시스템의 "엔진"으로 여기에 있습니다. 템플릿 자체에는 PHP가 없으며 'editable'키워드로 파일을 검색하여 업데이트 할 수있는 영역을 설정합니다. 나는 아무것도 저장하지 않고 대신 html 파일 자체에서 모든 것을 읽는 데이터베이스를 사용하고 싶지 않습니다.PHP가 html 파일의 '편집 가능'영역을 대체합니다.

여전히 수정해야 할 부분이 있지만 가장 중요한 것은 '편집 가능'영역의 배열을 반복하고 템플릿 파일을 업데이트하는 부분이 필요하다는 것입니다. 내가 폼 텍스트 영역의 설정을 통해 '편집'섹션 업데이트를 할 수 있도록하고 싶습니다

<html> 

<font class="editable"> 
This is editable section 1 
</font> 
<br><br><hr><br> 
<font class="editable"> 
This is editable section 2 
</font> 

</html> 

: 여기

는 test.html를 (테스트 용 템플릿 파일)입니다. 이것은 여전히 ​​약간의 작업이 필요하지만 여기까지 내가 가지고과 같습니다

<?php 

function g($string,$start,$end){ 
    preg_match_all('/' . preg_quote($start, '/') . '(.*?)'. preg_quote($end, '/').'/i', $string, $m); 
    $out = array(); 

    foreach($m[1] as $key => $value){ 
     $type = explode('::',$value); 
     if(sizeof($type)>1){ 
      if(!is_array($out[$type[0]])) 
      $out[$type[0]] = array(); 
      $out[$type[0]][] = $type[1]; 
     } else { 
      $out[] = $value; 
     } 
    } 
    return $out; 
}; 

// GET FILES IN DIR 
    $directory="Templates/"; 
    // create a handler to the directory 
    $dirhandler = opendir($directory); 
    // read all the files from directory 
    $i=0; 
    while ($file = readdir($dirhandler)) { 
     // if $file isn't this directory or its parent 
     //add to the $files array 
     if ($file != '.' && $file != '..') 
     { 
       $files[$i]=$file; 
      //echo $files[$i]."<br>"; 
      $i++;     
     } 
    }; 
//echo $files[0]; 

?> 

<div style="float:left; width:300px; height:100%; background-color:#252525; color:#cccccc;"> 
<form method="post" id="Form"> 
Choose a template: 
<select> 
<?php 
// Dropdown of files in directory 
foreach ($files as $file) { 
    echo "<option>".$file."</option>"; // do somemething to make this $file on selection. Refresh page and populate fields below with the editable areas of the $file html 
}; 
?> 
</select> 
<br> 
<hr> 
Update these editable areas:<br> 

<?php 


$file = 'test.html'; // make this fed from form dropdown (list of files in $folder directory) 
$html = file_get_contents($file); 

$start = 'class="editable">'; 
$end = '<'; 

$oldText = g($html,$start,$end); 

$i = 0; 
foreach($oldText as $value){ 
    echo '<textarea value="" style="width: 60px; height:20px;">'.$oldText[$i].'</textarea>'; // create a <textarea> that will update the editable area with changes 
    // something here 
    $i++; 
}; 

// On submit, update all $oldText values in test.html with new values. 
?> 
<br><hr> 
&nbsp;<input type="submit" name="save" value="Save"/> 
</div> 
<div style="float:left; width:300px;"> 
<?php include $file; // preview the file from dropdown. The editable areas should update when <textareas> are updated ?> 
</div> 
<div style="clear:both;"></div> 

나는이 대답은 조금 더 복잡 알고 있지만, 나는 어떤 도움을 정말 감사하겠습니다.

답변

1

달성하고자하는 바를 정확히 이해하고 있는지 확실하지 않습니다. 하지만 jquery에서 그렇게 할 것 같습니다.

당신은이 같은 "편집"클래스가 모든 HTML 요소를 얻을 수 있습니다 : 당신이 PHP에 모든 데이터가있는 경우

$(".editable").each(function(index){ 
    alert($(this).text()); // or .html() 
    // etc... do your stuff 
}); 

:

$(".editable") 

당신은 그들에 반복 할 수 있습니다 정렬. json을 사용하여 클라이언트에 전달하면됩니다. javascript 태그 안에 php print를 사용하십시오.

<?php 

print "var phparray = " . json_encode($myphparray); 

?> 

나는 클라이언트 측 (자바 스크립트)에 작업을 넣는 것이 더 좋을 것이라고 생각한다. 서버 작업로드 (PHP)가 낮아집니다.

하지만 내가 말했듯이, 나는 당신이 원하는 모든 것을 파악했다고 생각하지 않습니다.

+0

감사합니다. Lunfel, 시도해 보겠습니다. – John