Create a ProcessWire theme using AJAX integration

王林
Release: 2023-09-13 16:22:01
Original
832 people have browsed it

In this tutorial we will look at setting up a simple theme in ProcessWire, we will look at deferred output (now the default theme policy in ProcessWire) and set up our site to request new content using the following command: AJAX.

To accompany this tutorial, I created a new theme for ProcessWire using AJAX and deferred output, which can be found on Github. You can also find a demo of this theme here: Artist Profile Demo.

For instructions on installing ProcessWire and understanding the basics of AJAX, check out the following resources:

  • 使用 AJAX 集成创建 ProcessWire 主题使用 AJAX 集成创建 ProcessWire 主题 How to install and set up ProcessWire CMS 使用 AJAX 集成创建 ProcessWire 主题使用 AJAX 集成创建 ProcessWire 主题 Ben Byford January 18, 2016
  • 使用 AJAX 集成创建 ProcessWire 主题使用 AJAX 集成创建 ProcessWire 主题 Introduction to AJAX for Front-End Designers 使用 AJAX 集成创建 ProcessWire 主题使用 AJAX 集成创建 ProcessWire 主题 George Matsokos February 2, 2016

Direct output and delayed output

ProcessWire (PW) has an extremely flexible template system; it allows you to code any structure of your choice by creating a php file in the /site/templates folder and then in the ProcessWire admin The setup > templates > add new. In other words, there are two common template strategies in PW forums: Direct output and Delayed output.

Direct output

Directly output the command to see the specific page output in each php file. It would be great if each template was significantly different from the others. However, I find this cumbersome if only minor changes are required for each template. You may also find yourself copying from other templates or including numerous files. This is a very basic template (e.g. basic-page.php).

<?php 
include("./_head.php"); 

echo "Your html content";

include("./_foot.php"); 
?>
Copy after login

Delayed output

Delayed output will see public files named before after the template files (e.g. basic-page.php) and _main.php . Your _main. is used as a mothership for markup (html) and specific templates are relegated to adding content to predefined variable outputs in the _main.php file.

In the example below, I add the body and video fields from the template page to the variable $content and output the page markup in my _main.php file because it Always executed after.

Basic page.php:

<?php
    $content = $page->body . $page->video;
?>
Copy after login

_main.php:

<?php
    include("./head.inc");
?>

<div id="page">
	<header class="">
		<a id="logo" href="<?php echo $pages->get('/')->url; ?>">
			<h1>
				<?php echo $pages->get('/')->title; ?>
			</h1>
		</a>
	</header>
	<aside>
		<nav>
			<?php
                // echo nav links
				$children = $pages->get('/')->children;
				foreach($children as $child){		
					echo '<a name="'.$child->title.'" class="ajax-link'.$class.'" href="'. $child->url .'">'. $child->title .'</a>';
				}
			?>
		</nav>
	</aside>
	<div class="content-container cf">
		<div class="content current-content">
			<?php
				// add content to page from template file
				echo $content;
			?>
		</div>
	</div>
<?php
	include("./foot.inc");
?>
Copy after login

艺术家简介

艺术家简介是使用延迟输出的主题示例。主要 HTML 结构编写在 _main.php 文件中,包括页眉、页脚、徽标和导航。当前页面模板设置 $content 变量 - 例如我的 basic-page.php。

您可以安装 The Artist Profile 来查看我如何组合主题并在 main.js 文件中使用 AJAX。我现在将介绍该主题中的一些概念。

AJAX 数据策略

AJAX 允许我们的用户显示我们网站上的新内容,而无需重新加载徽标、页脚和导航等常见页面部分。这也意味着我们的用户在请求新页面时永远不会看到空白的白色浏览器窗口。

使用 AJAX,我们可以从我们的网站请求常见的数据类型,例如 HTML、JSON 或 XML。为了简单起见,我们将从我们的网站请求 HTML,但是,如果您创建或正在使用现有的前端模板库,我们可以请求 JSON,从而减少每个请求的数据量(您可以使用很多库)可以在前端使用,一个例子是小胡子)。

在我们的主题中,我希望徽标、导航和页脚保持不变,但当我们单击链接时,主要内容区域会动态(或异步)更改。

在 ProcessWire 主题中使用 AJAX

为此,我们需要创建两个容器 HTML 元素,我们可以在其中添加新内容。容器元素不会改变,但会保存内部元素和附加到其上的内容。新内容将被添加,同时旧内容将被动画化,然后被删除。这将创造出流畅的外观和感觉。

使用我的 _main.php 文件,容器如下所示:

<div class="content-container cf">
    <div class="content current-content">
		<?php
		echo $content;
		?>
	</div>
</div>
Copy after login

好的,到目前为止一切顺利。现在让我们添加一个对方便的 ProcessWire 变量 $ajax 的检查。 $ajax 声明请求是否来自 AJAX。下面是检查是否不是 AJAX 的示例:

if(!$ajax):
Copy after login

在我的主题的 _main.php 文件中,我可以包装 AJAX 请求不需要的内容,即除了 echo $content 之外的所有内容。看起来像这样:

<?php
// include page structure if not an ajax request
if(!$ajax):
    include("./head.inc");
?>

<div id="page">
	<header class="">
		<a id="logo" href="<?php echo $pages->get('/')->url; ?>">
			<h1>
			<?php echo $pages->get('/')->title; ?>
			</h1>
		</a>
	</header>
	<aside>
		<nav>
		<?php
		// nav content here	
		?>
		</nav>
	</aside>
	<div class="content-container cf">
		<div class="content current-content">
		<?php endif; // end if ajax ?>
		
        <?php
		// if ajax then only return $content
		echo $content;
		?>
		
        <?php if(!$ajax): ?>
		</div>
	</div>
<?php
    include("./foot.inc");
endif; // end if ajax
?>
Copy after login

现在我们已经准备好了模板,如果是普通页面请求,则可以为我们提供整个页面标记;如果是 AJAX 请求,则可以为我们提供 $content

使用 jQuery AJAX 在 JavaScript 中请求页面

我的主题使用 JavaScript 库 jQuery。在指向最新版本的 jQuery 库的链接之后,我在 foot.inc 中引用了 main.js 文件。

使用 jQuery 的 .on.ajax 函数,我们可以在每次单击带有类 .ajax-link

到目前为止,我们的 main.js 代码如下所示:

$(function() {

    var href;
	var title;

  	$('body').on('click','a.ajax-link',function(e) { // nav link clicked

      	href = $(this).attr("href");
      	title = $(this).attr("name");

		// load content via AJAX
      	loadContent(href);

		// prevent click and reload
    	e.preventDefault();
	});

	function loadContent(url){ // Load content

		// variable for page data
    	$pageData = '';

		// send Ajax request
    	$.ajax({
	        type: "POST",
	        url: url,
	        data: { ajax: true },
	        success: function(data,status){
				$pageData = data;
      		}
    	}).done(function(){ // when finished and successful

			// construct new content
      		$pageData = '<div class="content no-opacity ajax">' + $pageData + '</div>';

			// add content to page
			$('.content-container').append($pageData);

            // remove old content
            $('.content.current-content').remove();

            // show new content and clean up classes
            $(this).removeClass('no-opacity').removeClass('ajax').addClass('current-content');
            
    	}); // end of ajax().done()
  	} // end of loadContent()
});
Copy after login

上面的代码有一个 .on('click','a.ajax-link', function(){ OUR FUNCTIONS HERE }) ,它允许我们触发我们的 loadContent() 函数只要单击链接即可。在 loadContent() 函数中,我们使用链接中的 url 发送 ajax 请求,然后当 .done() 时,我们附加 data.content-container 元素。

以上所有内容都可以正常工作,但是我们可以添加许多额外的小功能以使一切变得无缝。

AJAX 技巧、技巧和逻辑

首先,我们可以对内容进行动画输入和输出(这链接到 main.js 文件的动画部分)。动画非常适合制作漂亮的网站,但也可以作为心理触发点来强调某些事情已经发生了变化。

接下来,我们要重新初始化页面所需的任何 JavaScript 函数,例如灯箱、幻灯片、砌体等,我们在将新的 HTML 数据添加到页面后将其放入。由于新内容是通过 AJAX 检索的,因此它可能不会与用于点击等的 JavaScript 侦听器绑定,因此除非我们重新初始化页面上所需的任何功能,否则不会触发。

创建已加载的检查对于防止无用的请求很有用。添加快速检查以查看新链接是否已被单击,然后返回 true; 如果它阻止用户多次访问同一页面。

最后,但可能是最重要的,我们现在可以使用 html5 历史记录来跟踪当前页面,并在用户按下后退按钮时重新加载过去的页面内容。

  • 使用 AJAX 集成创建 ProcessWire 主题使用 AJAX 集成创建 ProcessWire 主题 Use the History Web API for lovely, smooth page transitions 使用 AJAX 集成创建 ProcessWire 主题使用 AJAX 集成创建 ProcessWire 主题 Torik Firdaus April 28, 2016

Summarize

Using some of the above strategies, we can create a seamless AJAX experience for our website, and using ProcessWire, we can instantly integrate AJAX requests into our theme.

For more information about ProcessWire and AJAX, check out our Envato Tuts tutorial.

The above is the detailed content of Create a ProcessWire theme using AJAX integration. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!